在Android主屏幕上放置一个启动器图标(就像Google Play那样)

在Android主屏幕上放置一个启动器图标(就像Google Play那样),android,Android,我想使用我的应用程序删除第三方应用程序的启动程序图标 我有安装快捷方式权限: <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> 我认为使用getDrawableForDensity的解决方案不是最正确的。您应该让启动器决定,传递资源名称,如本例所示: private void createAppShortcut(String packageName) { P

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

我有安装快捷方式权限:

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

我认为使用
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);
}

你为什么要使用这种方法,因为google play自己会这样做。这将在每次应用程序启动时在主屏幕上生成图标。并且多次出现在主屏幕上。我使用此功能的方式与问题无关。我需要的是了解如何获得一个合适大小的应用程序图标。你需要按照android guidline在每个可绘制文件夹中放置特定分辨率的图标。LDPI应为36 x 36。MDPI应为48 x 48。TVDPI应为64 x 64。HDPI应为72 x 72。XHDPI应该是96 x 96。XXHDPI应为144 x 144。XXXHDPI应该是192 x 192。我指的是第三方应用程序图标,而不是我的应用程序图标。请参考此链接。它可能会对您有所帮助。
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);
}