6

下面是我的代码,用于创建所选应用程序的快捷方式。我真的没有问题,应用程序运行良好。

问题是我可以使用我的应用程序中的资源创建快捷方式:

    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));

但我真的很想要一个自定义的drawable。(可绘制 myDrawable=.....)

我能怎么做?

   ResolveInfo launchable=adapter.getItem(position);
   final Intent shortcutIntent = new Intent();
    ActivityInfo activity=launchable.activityInfo;
    ComponentName name=new ComponentName(activity.applicationInfo.packageName,activity.name);       
    shortcutIntent.setComponent(name);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, launchable.loadLabel(pm));
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));

    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
    finish();

非常感谢任何线索

4

2 回答 2

28

终于找到了解决办法;我愚蠢地使用 Intent.EXTRA_SHORTCUT_ICON_RESOURCE:

这是正确的代码:

Drawable iconDrawable = (....); 
BitmapDrawable bd = (BitmapDrawable) iconDrawable;
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());
于 2010-07-25T22:02:14.173 回答
3

我无法发表评论,因为我只有 23 名声望,但我使用了你的代码,它很有用。但是我的图像在快捷方式上没有正确缩放,所以我找到了两个可以完成解决方案的有趣主题:

要获得正确大小的快捷方式图标: https ://stackoverflow.com/a/19003905/3741926

缩放您的可绘制对象(使用您使用前一个函数计算的大小) https://stackoverflow.com/a/10703256/3741926

有了这一切,我得到了一个带有正确缩放图标的快捷方式,希望它会有所帮助

于 2014-11-25T22:49:24.393 回答