7

我想使用以下代码从我的小部件发送广播:

for (int i = 0; i < N; i++) {
   int appWidgetId = appWidgetIds[i];
   RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
   Intent x = new Intent();
   if (isActive == true) {
      views.setImageViewResource(R.id.w_image, R.drawable.w_bild);
      x.setAction("de.bulling.smstalk.DISABLE");
   } else {
      views.setImageViewResource(R.id.w_image, R.drawable.w_bild_off);
      x.setAction("de.bulling.smstalk.ENABLE");
   }
   PendingIntent z = PendingIntent.getBroadcast(context, 0, x, PendingIntent.FLAG_ONE_SHOT);
   views.setOnClickPendingIntent(R.id.w_image, z);
   appWidgetManager.updateAppWidget(appWidgetId, views);
}

但是,我收到此错误:

E/RemoteViews(18176): Cannot send pending intent: 
E/RemoteViews(18176): android.content.IntentSender$SendIntentException
E/RemoteViews(18176):   at android.app.ContextImpl.startIntentSender(ContextImpl.java:640)
E/RemoteViews(18176):   at android.widget.RemoteViews$SetOnClickPendingIntent$1.onClick(RemoteViews.java:157)
E/RemoteViews(18176):   at android.view.View.performClick(View.java:2408)
E/RemoteViews(18176):   at android.view.View$PerformClick.run(View.java:8816)
E/RemoteViews(18176):   at android.os.Handler.handleCallback(Handler.java:587)
E/RemoteViews(18176):   at android.os.Handler.dispatchMessage(Handler.java:92)
E/RemoteViews(18176):   at android.os.Looper.loop(Looper.java:123)
E/RemoteViews(18176):   at android.app.ActivityThread.main(ActivityThread.java:4627)
E/RemoteViews(18176):   at java.lang.reflect.Method.invokeNative(Native Method)
E/RemoteViews(18176):   at java.lang.reflect.Method.invoke(Method.java:521)
E/RemoteViews(18176):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
E/RemoteViews(18176):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/RemoteViews(18176):   at dalvik.system.NativeStart.main(Native Method

这是什么原因?你有什么主意吗?谢谢,马克

/edit:我找到了答案:它必须是 Intent x = new Intent(context, WidgetClass.class);

4

3 回答 3

11

我得到这个错误:

RemoteViews: Cannot send pending intent:  
RemoteViews: android.content.IntentSender$SendIntentException 
RemoteViews: at android.app.ContextImpl.startIntentSender()

解决方案是为每个(发送)PendingIntent 设置一个唯一的 requestCode,为此我使用提供的appWidgetId而不是每次都使用0 。

获得广播()

pendingActionIntent=PendingIntent.getBroadcast(this, widgetId , intentAction, pendingFlag);

获取活动()

pendingActionIntent=PendingIntent.getActivity(this, widgetId , intentAction, pendingFlag);

出于某种未知原因,它在文档中说“当前未使用”?

requestCode 发件人的私人请求代码(当前未使用)。

但它有效;)

于 2013-05-28T20:08:32.100 回答
2

我找到了答案:它必须是 Intent x = new Intent(context, WidgetClass.class);

于 2011-08-26T12:16:25.593 回答
0

我认为请求代码用于通过PendingIntent.FLAG_CANCEL_CURRENT和匹配更新和替换PendingIntent.FLAG_UPDATE_CURRENT

于 2017-12-12T09:28:56.283 回答