简体   繁体   English

从RemoteViewsFactory启动活动

[英]Start Activity from RemoteViewsFactory

i have an app launcher in an appwidget. 我在appwidget中有一个应用启动器。 I would to start Activitys from RemoteViewsFactory. 我将从RemoteViewsFactory启动Activity。 But it didn't work. 但这没有用。

Here is my Code from the RemoteViewsFactory: 这是我来自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;

}

And here is my Code from the TheWidgetProvider: 这是我来自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);
    }
}

Have anyone an idea, why it doesn't work? 有谁知道,为什么它不起作用?

The error is here: 错误在这里:

PendingIntent appsPendingIntent = PendingIntent.getBroadcast(context, 0, appsIntent, PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent.getBroadcast(...) creates a broadcast, you need to use PendingIntent.getActivity(...) like this: PendingIntent.getBroadcast(...)创建一个广播,您需要像这样使用PendingIntent.getActivity(...)

PendingIntent appsPendingIntent = PendingIntent.getActivity(context, 0, appsIntent, PendingIntent.FLAG_UPDATE_CURRENT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM