简体   繁体   English

即使按下了不同的实例小部件,onReceive也将始终接收最后一个appWidgetId

[英]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. 我试图在主屏幕上放置2个小部件实例。

Here is the log being printed, after 2 instance of widgets had been placed. 这是放置2个小部件实例后正在打印的日志。

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. 当我单击第一个窗口小部件,然后单击第二个窗口小部件时,我预计将分别打印170和171。 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 这是因为两个PendingIntent的处理方式相同,因此创建第二个将替换第一个,请参阅: 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. 因此,您的仅与Extra有所不同,这是行不通的。 Try to set something with setData. 尝试使用setData进行设置。

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

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

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