如何从主屏幕小部件中的网格视图启动新活动?

时间:2013-10-17 10:26:32

标签: android android-widget android-gridview

我是新手来创建一个小部件。我创建了一个小部件,在网格视图中显示所有已安装的应用程序我已经使用remoteviewfactory做了所有事情,但问题是我无法从小部件启动相应的应用程序。 我不知道是什么问题。我可以通过吐司感觉到点击,并且我也得到了包名,但我无法启动应用程序。

ApplicationInfo info = list.get(position);
            Intent mIntent = context.getPackageManager()
                    .getLaunchIntentForPackage(info.packageName);
            Toast.makeText(context, "Hello " + info.packageName,
                    Toast.LENGTH_SHORT).show();
            if (mIntent != null) {
                try {
                    context.startActivity(mIntent);
                } catch (ActivityNotFoundException err) {
                    Toast.makeText(context, "app not found",
                            Toast.LENGTH_SHORT).show();
                }
            }

请告诉我这是什么问题。

1 个答案:

答案 0 :(得分:0)

好的更新你的 onUpdate 方法

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

可以找到有关小部件的更多信息here