简体   繁体   English

如何在从appwidgetprovider的Pending Intent调用的活动中检测到appWidgetId

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

I have appwidgetprovider class as below which calls an activity to open 我有如下的appwidgetprovider类,它调用一个活动来打开

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) 我希望活动在我的活动类中接收widgetId,因此我通过putExtra()传递了它。此后,我一个接一个地添加了3 putExtra()部件,当我单击主屏幕上的任何小部件时,我只获得了最后一个的ID。小部件已添加(通过我的意图)

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. 我想要Android 2.2及以上版本。 Any suggestions are welcome 欢迎任何建议

Extras are not considered when comparing PedingIntent's, thus you get the same intent even if you change the extras. 比较PedingIntent时不考虑附加功能,因此即使更改附加功能,您也会获得相同的意图。 FLAG_UPDATE_CURRENT means that the current PendingIntent gets updated, thus you get the last ID only. FLAG_UPDATE_CURRENT表示当前的PendingIntent得到更新,因此仅获得最后一个ID。 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. 如果您想可靠地传递ID,则必须将其放入Intent的数据中,然后您将能够针对不同的ID获得不同的PendingIntent。 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. 然后从Intent的数据中提取ID,以区分小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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