Ho creato un widget Android.
In un emulatore Genymotion Nexus 4 con Android 4.4.4 funziona tutto bene.
Sul mio dispositivo Nexus 4 con Android 4.4.4, metto il widget sulla schermata iniziale e si trasforma nel widget dell'app Google.
Quindi si trasforma nel mio widget, quindi di nuovo nel widget dell'app Google e così via. Lo fa finché non rimuovo il widget dalla schermata principale.
Inoltre sto usando parse.com come memoria online e sul mio telefono i dati non sembrano essere ottenuti. Non posso dirlo con certezza perché il widget continua a cambiare.
Il mio widget contiene 3 file.
Uno che estende la classe di applicazione:Il widget Android funziona bene nell'emulatore ma sul telefono si trasforma nel widget dell'app Google
public class MyApp extends Application
{
private MyModel model;
@Override
public void onCreate() {
Parse.enableLocalDatastore(this);
ParseObject.registerSubclass(GdbdData.class);
Parse.initialize(this, "-", "-");
if(model == null) {
Intent intent = new Intent(this,GdbdWidgetProvider.class);
intent.setAction(WidgetUtils.WIDGET_REFRESH_STORAGE);
intent.putExtra("userId",123);
sendBroadcast(intent);
}
super.onCreate();
}
Uno WidgetProvider:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
final String action = intent.getAction();
if (action.equals(WidgetUtils.WIDGET_REFRESH_STORAGE))
{
MyApp app = ((MyApp)context.getApplicationContext());
int userId = intent.getIntExtra("userId",0);
TryGetModelFromRemoteStorage(userId,context);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
MyApp app = ((MyApp)context.getApplicationContext());
MyModel model = app.getModel();
// Construct the RemoteViews object
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.the_widget);
PendingIntent clickerPendingIntent = buildButtonPendingIntent(context);
// I change some textviews and imageviews inside the widget here
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
// initiate widget update request
Intent intent = new Intent();
intent.setAction(WidgetUtils.WIDGET_UPDATE_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
return pendingIntent;
}
E uno broadcastreceiver che gestisce un pulsante sul widget:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(WidgetUtils.WIDGET_UPDATE_ACTION)) {
updateWidget(context);
}
}
private void updateWidget(Context context) {
MyApp app = ((MyApp)context.getApplicationContext());
MyModel model = app.getModel();
//I update some fields in the model based on some business rules at this point
MyWidgetProvider.SendUpdateMessageToWidgets(context);
}
Qualcuno sa quello che sono fare male?
Edit 1: Aggiunta di file manifesto come richiesto
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app.main" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".GdbdWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/the_widget_info" />
</receiver>
<receiver
android:name=".TapMarkDayIntentReceiver"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.my.app.intents.UPDATE_WIDGET" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/the_widget_info" />
</receiver>
</application>
</manifest>
Edit 2, aggiunto widget di file xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="140dp" android:minHeight="140dp"
android:updatePeriodMillis="1000000"
android:previewImage="@drawable/example_appwidget_preview"
android:initialLayout="@layout/the_widget" android:widgetCategory="home_screen"
android:initialKeyguardLayout="@layout/the_widget"></appwidget-provider>
pubblica il tuo manifest –
@DavidWasser lo ha aggiunto ora – Para
Per favore pubblica anche "the_widget_info.xml'. –