【发布时间】:2019-02-20 20:24:12
【问题描述】:
我已经为此发疯了。
所以,我创建了一个带有 ListView 的小部件。在列表视图中的每个项目上,我都希望有一个简单的按钮,它可以“调用”带有特定参数的 BroadcastReceiver。
AppWidgetProvider
override fun onUpdate(context: Context?, appWidgetManager: AppWidgetManager?, appWidgetIds: IntArray?) {
// Getting the RemoteView, setting adapter etc
val myIntentTemplate = Intent(context, MyReceiver::class.java)
val myPendingIntentTemplate = TaskStackBuilder.create(context)
.addParentStack(MainActivity::class.java)
.addNextIntent(myIntentTemplate)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
remoteViews.setPendingIntentTemplate(R.id.listview, myPendingIntentTemplate)
// Updating app widget
}
清单
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
</receiver>
在RemoteViewsFactory 中,我只是创建了一个带有BroadcastReceiver 参数的FillInIntent,并将其作为OnClickFillInIntent 添加到我的按钮中。这很好,因为小部件被创建和加载。但是当我点击按钮时,我得到了这个错误:
09-15 01:53:02.160 1961-3674/system_process I/ActivityManager: START u0 {flg=0x1000c000 cmp=com.mydomain.myapp/.MyReceiver bnds=[864,524][984,596] (has extras)} from uid 10119
09-15 01:53:02.160 2581-2581/com.google.android.apps.nexuslauncher E/RemoteViews: Cannot send pending intent due to unknown exception:
android.content.ActivityNotFoundException: No Activity found to handle null
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2007)
at android.app.Activity.startIntentSenderForResultInner(Activity.java:4845)
at android.app.Activity.startIntentSenderForResult(Activity.java:4812)
at com.android.launcher3.Launcher.startIntentSenderForResult(SourceFile:1356)
at android.app.Activity.startIntentSender(Activity.java:4997)
所以,基本上它说它没有找到com.mydomain.myapp/.MyReceiver。
我很确定我的错误是在创建意图时。此外,PendingIntent 明确需要一个活动。但我不想在点击时启动 Activity。
【问题讨论】:
标签: android kotlin android-widget android-broadcastreceiver