Android widget not updating - has incorrect widget

2019-02-21 13:55发布

问题:

I've been working on a widget and making some advances (I hope) but still can't get it to do what I want.

I have a configurator activity which works ok. When it closes I call the updateAppWidget method in the WordWidget class and the widget updates the random number.

What I can't do it get the random number to change because of onClick. According to the log I do enter the onReceive() method, but thats about it.

In the log I'm printing the widget ID. I noticed that when run from the configurator activity it's printing appWidgetId=33, but when I press the widget it prints appWidgetId=24.

This is the code:

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);
    }
}

This is the log file, from when I touch the widget (note that the prefs activity also has a log printout and shows the widget ID as a different number that this):

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): In updateAppWidget(), nextNumb = 42

So as you can see, I'm actually entering my updateAppWidget method, but for some reason the appWidgetId is incorrect.

Any ideas why this is happening? Thanks a lot!

回答1:

In your function updateAppWidget go to this line:

PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, 0);

...and change it into:

PendingIntent widgetPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, widgetIntent, 0);


回答2:

For Updating Your Home Screen widget on Touch You need to Register a custom broadcast receiver in Manifest and add it as action with widget as:

Step 1: Register Custom Receiver as:

<intent-filter>
<action android:name="com.imrankhanandroid.HomeWidgetRandomNum.ACTION_WIDGET_RANDOM"/>
</intent-filter>

Step 2: In Your Widget Class AddAction to layout as:

public void onReceive(Context paramContext, Intent paramIntent)
    {
        //Intent
         String str = paramIntent.getAction();
         if (paramIntent.getAction().equals(ACTION_WIDGET_RANDOM)) {
             updateWidgetState(paramContext, str);   
         }
         else
            {
              super.onReceive(paramContext, paramIntent);
            }
    }

STEP 3: In Your updateWidgetState add new action to Intent as:

rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layoutmain);
Intent active = new Intent(paramContext, Randomnuberwidget.class);
active.setAction(ACTION_WIDGET_RANDOM);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(paramContext, 0, active, 0);
rview.setOnClickPendingIntent(R.id.ImageView_gps, actionPendingIntent);

Finally you can Download Working HomeWidget Example from HERE HomeWidgetRendomNumber Which Generating Random Number On Every Click.



回答3:

You need to set a last flag in a getBroadcast: PendingIntent.FLAG_UPDATE_CURRENT