我在appwidget中有一个应用程序启动器。我想从RemoteViewsFactory启动Activity。 但它没有用。
这是RemoteViewsFactory的代码:
@Override
public RemoteViews getViewAt(int position) {
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.app_item);
ResolveInfo info = mApps.get(position);
Bundle extras = new Bundle();
extras.putString(TheWidgetProvider.APP_ID, info.getClass().getName());
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.app_name, fillInIntent);
return rv;
}
这是我在TheWidgetProvider中的代码:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent AppsServiceIntent = new Intent(context, AppsService.class);
AppsServiceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
AppsServiceIntent.setData(Uri.parse(AppsServiceIntent.toUri(Intent.URI_INTENT_SCHEME)));
views.setRemoteAdapter(R.id.GridViewApps, AppsServiceIntent);
Intent appsIntent = new Intent(context, TheWidgetProvider.class);
appsIntent.setAction(TheWidgetProvider.EVENT_SHOW_APP);
appsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
appsIntent.setData(Uri.parse(updateServiceIntent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent appsPendingIntent = PendingIntent.getBroadcast(context, 0, appsIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.GridViewApps, appsPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
有人知道,为什么它不起作用?
答案 0 :(得分:2)
错误在于:
PendingIntent appsPendingIntent = PendingIntent.getBroadcast(context, 0, appsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent.getBroadcast(...)
创建广播,您需要使用PendingIntent.getActivity(...)
,如下所示:
PendingIntent appsPendingIntent = PendingIntent.getActivity(context, 0, appsIntent, PendingIntent.FLAG_UPDATE_CURRENT);