0

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

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

请告诉我有什么问题。

4

1 回答 1

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

可以在此处找到有关小部件的更多信息

于 2013-10-17T10:53:56.957 回答