创建快捷方式:如何使用drawable作为Icon?

时间:2010-07-21 11:56:43

标签: android icons android-intent shortcut drawable

以下是我创建所选应用程序快捷方式的代码。我真的没问题,应用程序运行得很好。

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

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

但我真的想要一个自定义的drawable。 (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();

非常感谢任何线索

2 个答案:

答案 0 :(得分:28)

答案 1 :(得分:3)

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

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

缩放您的drawable(使用您使用上一个函数计算的大小) https://stackoverflow.com/a/10703256/3741926

尽管如此,我得到了一个带有正确缩放图标的快捷方式,希望它能提供帮助