I want use my app to drop a launcher icon of a 3rd party app.

我想使用我的应用程序删除第三方应用程序的启动器图标。

I have the INSTALL_SHORTCUT permission:

我有INSTALL_SHORTCUT权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

I am able to drop the shortcut, however, the icon appears pixelated.

我能够删除快捷方式,但图标显示为像素化。

What is the way to get an icon in the size appropriate to the home screen? (like the one Google Play drop after installation)

获取适合主屏幕大小的图标的方法是什么? (就像安装后的Google Play一样)

*I use the following code to get the icon Bitmap for the shortcut:

*我使用以下代码获取快捷方式的图标位图:

public static Drawable getInstalledPackageIcon(Context context, String packageName) {

    PackageManager packageManager = context.getPackageManager();

    try {

        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);

        if (applicationInfo != null) {
            Drawable iconDrawable = applicationInfo.loadIcon(packageManager);
            return iconDrawable;
        }

    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

1 个解决方案

#1


I don't think the solution with the use of getDrawableForDensity is the most correct. You should let the launcher decide, passing the resource name as you can see in this example:

我不认为使用getDrawableForDensity的解决方案是最正确的。您应该让启动器决定,传递资源名称,如您在此示例中所示:

private void createAppShortcut(String packageName) {

    PackageManager packageManager = getPackageManager();

    ApplicationInfo appInfo = null;
    Resources resources = null;

    try {
        appInfo = packageManager.getApplicationInfo(packageName, 0);
        resources = packageManager.getResourcesForApplication(packageName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return;
    }

    Intent shortcutIntent = packageManager.getLaunchIntentForPackage(packageName);
    CharSequence shortcutName = appInfo.loadLabel(packageManager);

    Intent.ShortcutIconResource shortcutIconResource = new Intent.ShortcutIconResource();
    shortcutIconResource.packageName = packageName;
    shortcutIconResource.resourceName = resources.getResourceName(appInfo.icon);

    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
    shortcut.putExtra("duplicate", false);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIconResource);
    sendBroadcast(shortcut);
}

更多相关文章

  1. 封面流:做无限循环(像启动器,当到达最后一项时可以回到第一项)
  2. Android实现桌面图标通知数量角标
  3. android MPChart图标使用详解
  4. Eclipse工程图标左下角报错

随机推荐

  1. 在Windows Mobile和MySQL之间同步DB
  2. 如何使用mysqldump在帐户之间复制视图?
  3. MySQL数据库引擎简介
  4. 如何使用MySql和PHP存储/处理多个表的数
  5. 使用cmake安装mysql5.5.13
  6. Linux测试环境搭建apache+mysql+php
  7. 如何使用本地phmyadmin客户端访问远程服
  8. Failed while installing Dynamic Web Mo
  9. MySQLDriverCS Exception: MySQLDriverCS
  10. Mysql字符集和校验规则