Brak aktualizacji widgetu Android - ma nieprawidłowy identyfikator widgetu
Pracowałem nad widżetem i robiłem pewne postępy (mam nadzieję), ale wciąż nie mogę tego zrobić, aby robić to, co chcę.
Mam aktywność konfiguratora, która działa dobrze. Po zamknięciu wywołuję metodę updateAppWidget w klasie WordWidget, a widget aktualizuje liczbę losową.
Co nie mogę zrobić, to zmienić losową liczbę z powodu onClick. Zgodnie z logiem wprowadzam metodę onReceive (), ale o to chodzi.
W dzienniku drukuję identyfikator widgetu. Zauważyłem, że po uruchomieniu z działania konfiguratora drukuje appWidgetId = 33, ale po naciśnięciu widgetu drukuje appWidgetId = 24.
To jest kod:
public class WordWidget extends AppWidgetProvider
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
//This is run when a new widget is added or when the update period expires.
Log.v("wotd", "In onUpdate(), updating " + appWidgetIds.length + " widgets");
Log.v("wotd", " the array is " + appWidgetIds.toString());
for(int x = 0; x < appWidgetIds.length; x++)
{
Integer appWidgetId = appWidgetIds[x];
//Method that updates the random value
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
Log.v("wotd", "In onReceive() with intent=" + intent.toString());
if (intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE"))
{
Integer mAppWidgetId;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
Bundle extras = intent.getExtras();
if(extras != null)
{
Log.v("wotd", "In onReceive(), extras is valid");
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
//If they gave us an intent without a valid widget Id, just bail.
if (mAppWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
Log.v("wotd", "In onReceive(), mAppWidgetId is ok");
updateAppWidget(context, appWidgetManager, mAppWidgetId);
}
} else
{
Log.v("wotd", "In onReceive(), extras is INVALID");
mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
}
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, Integer appWidgetId)
{
Log.v("wotd", "In updateAppWidget() with appWidgetId=" + appWidgetId);
//Setup onClick
Intent widgetIntent = new Intent(context, WordWidget.class);
widgetIntent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);
Integer nextNumb = new Random().nextInt(100);
Log.v("wotd", "In updateAppWidget(), nextNumb = " + nextNumb.toString());
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
remoteView.setTextViewText(R.id.mainText, String.valueOf(nextNumb));
remoteView.setOnClickPendingIntent(R.id.mainText, widgetPendingIntent);
// Tell the widget manager
appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}
}
Jest to plik dziennika, z którego dotykam widżetu (zwróć uwagę, że działanie prefs ma również wydruk dziennika i pokazuje identyfikator widgetu jako inny numer niż ten):
05-14 17:15:47.453: V/wotd(1585): In onReceive() with intent=Intent { act=android.appwidget.action.APPWIDGET_UPDATE flg=0x10000000 cmp=com.rfxlabs.wordofthedaywidget/.WordWidget bnds=[202,177][222,201] (has extras) }
05-14 17:15:47.453: V/wotd(1585): In onReceive(), extras is valid
05-14 17:15:47.463: V/wotd(1585): In onReceive(), mAppWidgetId is ok
05-14 17:15:47.463: V/wotd(1585): In updateAppWidget() with appWidgetId=24
05-14 17: 15: 47.463: V / wotd (1585): W updateAppWidget (), nextNumb = 42
Tak więc, jak widać, faktycznie wprowadzam metodę updateAppWidget, ale z jakiegoś powodu appWidgetId jest niepoprawny.
Jakieś pomysły, dlaczego tak się dzieje? Wielkie dzięki!