【问题标题】:Widget to create Shortcut of Installed Apps用于创建已安装应用程序快捷方式的小部件
【发布时间】:2012-03-16 01:18:31
【问题描述】:

如何从我的小部件创建预安装应用程序的快捷方式?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    您想要一些静态快捷方式吗?只是预装的应用?

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

    RemoteViewsWidget views = new RemoteViewsWidget(context, R.layout.your_layout);
    

    其中 RemoteViewsWidget 是 RemoteViews 的子类。您会希望上下文用于在 RemoteViewsWidget 类中完成的操作。

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

    setImageViewUri(imageResId, uri);
    setTextViewText(textResId, displayName);
    

    其中 imageResId 是布局中您希望成为缩略图的资源,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);
    

    获取启动器中所有应用的解析信息列表。您可以从该列表中获取您需要的所有内容,以便为上面的 setOnClickPendingIntent 制定意图。如果您只需要这些应用程序的子集,则可以应用不同或更多类别、不同操作等。或者您可以只使用已知的包或活动。

    创建远程视图后,您需要在实用程序方法或小部件提供程序子类中更新小部件:

    views.updateWidget(); // update resources with image, text and intent as above
    AppWidgetManager.getInstance(context.getApplicationContext()).updateAppWidget(
            widgetId, views);
    

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多