疑问如题:
Intent intent = new Intent(context, MyWidgetProvider.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); PendingIntent doubleClick = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); appWidgetManager.updateAppWidget(appWidgetId, remoteViews); --------------------------------> int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
这是我遇到的问题,虽然通过修改getBroadcast方法的最后一个参数是int flag为PendingIntent.FLAG_UPDATE_CURRENT避免了在传送过程中出现的数据为空,但是得到的数据却总是Home screen中的最后一个该包的widgetID。此疑问未解。
问题解决,代码如下:
Intent intent = new Intent(context, MyWidgetProvider.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); Log.i(TAG, "appWidgetId "+appWidgetId); PendingIntent doubleClick = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); remoteViews.setOnClickPendingIntent(R.id.widget, doubleClick); appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
也就是将getBroadcast的第二个参数修改为我们要传递的appWidgetId即可。