我创建的Widget
有一个Button
,而下面有一个ListView
。单击按钮时,我想打开Activity
,但似乎无效,我不知道为什么。
以下是我的AppWidgetProvider
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
Intent configIntent = new Intent(context, NewActivity.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.btn_activity, configPendingIntent);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
svcIntent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
return remoteViews;
}
我认为这是因为我的updateWidgetListView
,我尝试将Intent放在updateWidgetListView
内,但单击按钮时仍然无效。
提前谢谢你。
答案 0 :(得分:0)
在我的onUpdate
我在widget_layout
iteration
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// initializing widget layout
RemoteViews rvBtn = new RemoteViews(context.getPackageName(),
R.layout.home_widget);
// register the button event
rvBtn.setOnClickPendingIntent(R.id. btn_activity,
buildButtonPendingIntent(context));
final int N = appWidgetIds.length;
for (int i = 0; i < N; ++i) {
//initialize another RemoteViews for ListView
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
然后创建BroadcastReceiver
,我将调用Intent
并在receiver
上添加AndroidManifest
。