Android AppWidgetProvider 中 ListView 点击事件参数传递
在 AppWidgetProvider 中,ListView 点击事件的参数可以通过 PendingIntent 的 extras 参数传递。具体步骤如下:
-
在 RemoteViews 中为 ListView 中的每个 Item 设置一个 PendingIntent,并将需要传递的参数设置到 PendingIntent 的 extras 中。
-
在 AppWidgetProvider 中,重写 onReceive 方法。当接收到 PendingIntent 的点击事件时,可以通过 Intent 的 getExtras 方法获取到传递的参数。
示例代码如下:
// RemoteViews 中为 ListView Item 设置 PendingIntent
Intent intent = new Intent(context, MyWidgetService.class);
intent.putExtra('param', 'value');
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.list_item, pendingIntent);
// AppWidgetProvider 中处理 PendingIntent 点击事件
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals('xxx.xxx.xxx.ACTION_LIST_ITEM_CLICK')) {
String param = intent.getExtras().getString('param');
// 处理参数
}
}
原文地址: https://www.cveoy.top/t/topic/oI5t 著作权归作者所有。请勿转载和采集!