Android 用于创建已安装应用程序快捷方式的小部件

Android 用于创建已安装应用程序快捷方式的小部件,android,android-widget,Android,Android Widget,如何从my widget为预装应用程序创建快捷方式?是否需要一些静态快捷方式?只是预装的应用程序 这就是我在主屏幕上创建快捷方式的方式: RemoteViewsWidget views = new RemoteViewsWidget(context, R.layout.your_layout); 其中RemoteViewsWidget是RemoteView的子类。您将希望上下文用于在RemoteViewsWidget类中完成的操作 在RemoteViewsWidget类中的一个方法中,我为“快

如何从my widget为预装应用程序创建快捷方式?

是否需要一些静态快捷方式?只是预装的应用程序

这就是我在主屏幕上创建快捷方式的方式:

RemoteViewsWidget views = new RemoteViewsWidget(context, R.layout.your_layout);
其中RemoteViewsWidget是RemoteView的子类。您将希望上下文用于在RemoteViewsWidget类中完成的操作

在RemoteViewsWidget类中的一个方法中,我为“快捷方式”设置了图像和文本:

其中imageResId是布局中您希望成为thumnail的资源,uri指向图像(我使用本地保存的文件作为图像源)。textResId和displayName类似

setOnClickPendingIntent(layoutViewId, PendingIntent.getActivity(
        context, requestCode, intent, flags));
这里,layoutViewId是图像视图和文本视图所在的父布局。此布局是在选中时触发意图的布局。挂起的意图是在选择布局时执行的-只要填写您的上下文、要启动的活动的意图和标志(如果需要)

要从应用程序中找到启动它们所需的内容,您可能需要使用PackageManager:

List<ResolveInfo> appInfos = context.getPackageManager().queryIntentActivities(
        new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER), 0);
List<ResolveInfo> appInfos = context.getPackageManager().queryIntentActivities(
        new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER), 0);
views.updateWidget(); // update resources with image, text and intent as above
AppWidgetManager.getInstance(context.getApplicationContext()).updateAppWidget(
        widgetId, views);