我在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);
    }
}


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

最佳答案

错误在这里:

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);

关于java - 从RemoteViewsFactory启动 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24912544/

10-08 20:34