简体   繁体   中英

onReceive will always receive the last appWidgetId, even different instance widget is being pressed

I have the following code. I tried to place 2 instance of widgets in my home screen.

Here is the log being printed, after 2 instance of widgets had been placed.

onUpdate START
onUpdate 170
onUpdate START
onUpdate 171

When I click on 1st widget followed by 2nd widget, I expect 170 and 171 will be printed respectively. However, this is what I get.

onReceive 171  <-- I'm expecting 170 to be printed.
onReceive 171

Is there anything wrong with my code? Or, I'm having wrong expectation?

public class MyAppWidgetProvider extends AppWidgetProvider {    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.i("CHEOK", "onUpdate START");

        for (int appWidgetId : appWidgetIds) {
            Log.i("CHEOK", "onUpdate " + appWidgetId);

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout_inverse_holo_light);

            // Register an onClickListener
            Intent refreshIntent = new Intent(context, JStockAppWidgetProvider.class);
            refreshIntent.setAction(REFRESH_ACTION);
            refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);            
            remoteViews.setOnClickPendingIntent(R.id.refresh_button, refreshPendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        AppWidgetManager mgr = AppWidgetManager.getInstance(context);
        if (intent.getAction().equals(REFRESH_ACTION)) {
            int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

            Log.i("CHEOK", "onReceive " + appWidgetId);

            // How can I access my remoteView?
            // During onUpdate, is saving all the remoteViews in local member
            // Map<appWidgetId, remoteView> a good idea?
        }
        super.onReceive(context, intent);
    }

    private static final String REFRESH_ACTION = "org.yccheok.jstock.gui.widget.MyAppWidgetProvider.REFRESH_ACTION";  
}

This is because both your PendingIntent are treated the same, hence creating the second one will replace the first one, refer to: http://developer.android.com/reference/android/content/Intent.html#filterEquals%28android.content.Intent%29

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

So yours is only different by the Extra, which doesn't work. Try to set something with setData.

refreshIntent.setData(Uri.parse(refreshIntent.toUri(Intent.URI_INTENT_SCHEME)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM