简体   繁体   中英

how to detect appWidgetId in activity called from Pending Intent of appwidgetprovider

I have appwidgetprovider class as below which calls an activity to open

for (int widgetId : allWidgetIds) {

        remoteViewParent = new RemoteViews(context.getPackageName(),R.layout.widget_initial_layout);

        try{
            Intent clickIntent = new Intent(context,Activity.class);//same class name is passed to give the call to itself
            clickIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                            //PASSING ID HERE
            clickIntent.putExtra("checkintent", ""+widgetId);


            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViewParent.setOnClickPendingIntent(R.id.widget_img_logo, pendingIntent);

            appWidgetManager.updateAppWidget(widgetId, remoteViewParent);

        }catch (Exception e) {
        }

    }

I want the activity to recieve the widgetId in my Activity Class so I pass it via putExtra() After this I add 3 widgets one by one and when I click on any of the Widgets on my Home Screen I just get the ID of the Last Widget Added(via my intent)

Is there a way to distinguish clicks on a particular widget added, any work around anything?

Caught up in this since long.

I want this for android 2.2 and above. Any suggestions are welcome

Extras are not considered when comparing PedingIntent's, thus you get the same intent even if you change the extras. FLAG_UPDATE_CURRENT means that the current PendingIntent gets updated, thus you get the last ID only. If you want to pass the ID reliably, you have to put it into the Intent's data, then you will be able to get different PendingIntent's for different ID's. Something like this:

Uri data = Uri.withAppendedPath (Uri.parse("mywidget://mywidget/widgetId/#"),
                String.valueOf(widgetId));
intent.setData(data);

Then extract the ID from the Intent's data to distinguish between widgets.

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