我希望我的窗口小部件显示一个列表,其中包含可点击按钮和ListItem上包含按钮的可点击背景。我创建了一个区分不同事件的服务,但是如果按钮上设置了onClickFillIntent
,则永远不会注册背景上的点击。
(我已尝试在其他内容上设置多个onClickFillIntents
,例如TextView
和一个按钮,这可以完美无缺。一切但背景有效。)
TLDR;
为什么intent1
有效,但intent2
没有?
logcat没有错误。
widget.kt
class Widget() : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, Ids: IntArray) {
for (i in Ids.indices) {
val views = RemoteViews(context.getPackageName(), R.layout.classificationwidget)
views.setRemoteAdapter(R.id.widget_list, context.intentFor<WidgetService>())
views.setPendingIntentTemplate(R.id.widget_list, PendingIntent.getService(context,0,context.intentFor<DataCollectorService>("someAction"), PendingIntent.FLAG_CANCEL_CURRENT))
appWidgetManager.updateAppWidget(Ids[i], views)
}
}
}
class WidgetService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
return WidgetViews(this.applicationContext)
}
}
class WidgetViews(private val context: Context) : RemoteViewsService.RemoteViewsFactory {
override fun getViewAt(position: Int): RemoteViews? {
val views = RemoteViews(context.packageName, R.layout.widgetlistitem)
val intent1 = context.intentFor<MyService>(ACTION to "1")
views.setOnClickFillInIntent(R.id.widget_button, intent1)
val intent2 = context.intentFor<MyService>(ACTION to "2")
views.setOnClickFillInIntent(R.id.widget_item_background, intent2)
return views
}
... (rest of the methods)
}
widgetlistitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_item_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:background="@drawable/card"
android:clickable="true"
android:focusable="true">
<Button
android:id="@+id/widget_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_margin="8dp"
android:text="Jetzt" />
</RelativeLayout>
widget_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/widgetlistitem">
</ListView>