无法从小部件发送挂起意图,SendIntentException

发布于 2024-12-01 19:43:14 字数 2075 浏览 2 评论 0原文

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

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

这是什么原因?你有什么想法吗? 谢谢, 马克

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

I want to send a broadcast from my widget with this code:

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);
}

However, I get this error:

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

What's the reason for this? Do you have any idea?
Thanks,
Marc

/edit: I found the answer: It has to be Intent x = new Intent(context, WidgetClass.class);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄风 2024-12-08 19:43:14

我也遇到了这个错误:

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

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

getBroadCast()

pendingActionIntent=PendingIntent.getBroadcast(this, widgetId,
意图操作,待定标志);

getActivity()

pendingActionIntent=PendingIntent.getActivity(this, widgetId,
意图动作,待定标志);

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

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

但它有效;)

I got this error too:

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

The solution was to set a unique requestCode for each (sending) PendingIntent, and for this I used the provided appWidgetId instead of just using a 0 each time.

getBroadCast()

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

getActivity()

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

For some unknown reason it says it "currently not used" in the documentation?

requestCode Private request code for the sender (currently not used).

but it works;)

提赋 2024-12-08 19:43:14

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

I found the answer: It has to be Intent x = new Intent(context, WidgetClass.class);

囚你心 2024-12-08 19:43:14

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

I think the request codes are used to match updates and replacements via PendingIntent.FLAG_CANCEL_CURRENT and PendingIntent.FLAG_UPDATE_CURRENT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文