如你所知,当应用程序正常安装时,会在launcher菜单屏幕上创建图标。我想做的是在安装过程中在用户主屏幕上创建图标。(不按住图标5秒钟。)
我从另一个来源听到这个,只是补充一下
<category android:value="android.intent.category.HOME" />转到AndroidManifest.xml文件,但它不起作用。
还有没有别的办法呢?
发布于 2011-09-18 21:24:20
您可以使用以下命令:
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("packageName", "className");
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "shortcut_name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
//intent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(addIntent);您必须在AndroidManaifest.xml中使用以下权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />您可以根据需要使用注释过的代码。
请注意,上面的API可能没有文档记录。但它是有效的。
发布于 2014-07-09 14:55:41
现在,Google Play服务解决了这一问题。你不再需要添加任何代码来完成这项工作。现在,当你从Google Play商店安装一个应用程序时,它会自动在主屏幕上创建徽标。它可以在Google Play商店设置中处理。例外:如果您正在使用任何自定义roms或启动器,它不能与一些工作。
发布于 2012-09-26 18:30:54
我使用这些方法来正确地添加或删除快捷键。当用户手动添加/删除快捷方式时,这些方法工作得很好,并且与安卓系统的相同。
public static void addShortcut(Context context)
{
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
ApplicationInfo appInfo = context.getApplicationInfo();
// Shortcut name
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);
shortcut.putExtra("duplicate", false); // Just create once
// Setup activity shoud be shortcut object
ComponentName component = new ComponentName(appInfo.packageName, appInfo.className);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component));
// Set shortcut icon
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(context, appInfo.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
context.sendBroadcast(shortcut);
}
public static void deleteShortcut(Context context)
{
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
ApplicationInfo appInfo = context.getApplicationInfo();
// Shortcut name
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appInfo.name);
ComponentName comp = new ComponentName(appInfo.packageName, appInfo.className);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
context.sendBroadcast(shortcut);
}权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />https://stackoverflow.com/questions/4854197
复制相似问题