如何在启动器主屏幕上放置应用程序图标?

res*_*ree 16 android

如您所知,当正常安装应用程序时,会在启动器菜单屏幕上创建图标.我想要做的是在安装过程中在用户主屏幕上创建图标.(不按图标5秒钟.)

我从另一个来源听到这个只是添加

<category android:value="android.intent.category.HOME" />
Run Code Online (Sandbox Code Playgroud)

到AndroidManifest.xml文件,但它没有用.

还有其他办法吗?

Vas*_*asu 16

你可以用这个:

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);
Run Code Online (Sandbox Code Playgroud)

您必须在AndroidManaifest.xml中使用以下权限

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Run Code Online (Sandbox Code Playgroud)

您可以根据自己的要求使用注释代码.

请注意,上面的API可能没有记录.但它的确有效.

  • 要卸载快捷方式,请参阅我的答案http://stackoverflow.com/questions/3480287/trying-to-uninstall-shortcut-but-shortcut-wont-go-away/ (3认同)
  • 有用。但我认为选项:intent.putExtra("duplicate", false); 默认情况下应该打开以避免主屏幕上的重复图标 (2认同)

Ama*_*yan 10

现在,Google Play服务已解决了这个问题.您不必再添加任何代码来执行此操作.现在,当您从Google Play商店安装应用时,它会自动在主屏幕中创建徽标.它可以在Google Play商店设置中处理.例外:如果您使用任何自定义ROM或发射器,它不适用于某些.


Chr*_*CVB 7

我使用这些方法来正确添加或删除快捷方式.当用户手动添加/删除快捷方式时,这些方法运行良好,与Android系统相同.

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);
}
Run Code Online (Sandbox Code Playgroud)

权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Run Code Online (Sandbox Code Playgroud)

BroadcastReceiver:

<receiver android:name="YOUR.PACKAGE.PackageReplacedReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED" />
        <data android:scheme="package" android:path="YOUR.PACKAGE" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)