1

我在应用小部件中有一个应用启动器。我会从 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);
    }
}

有谁知道,为什么它不起作用?

4

1 回答 1

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);
于 2014-07-23T14:17:01.833 回答