在Android主屏幕上删除启动器图标(就像Google Play一样)
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);
}