android - 安装应用程序时如何自动将应用程序启动器图标添加到主屏幕(android)

标签 android android-launcher

我创建了一个 Android 应用程序。我想要的是,当我安装我的应用程序时,它应该自动将快捷方式/启动器图标添加到主屏幕。

当我安装应用程序时,应自动创建启动器图标。

我尝试过这个:但它不起作用。

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

最佳答案

要做到这一点,你需要逆向 Android,因为主屏幕在用户控制下,但仍然有办法 只需在 oncreate 方法外部创建一个方法 createShortCut() 并在 onCreate(Bundle savingInstanceState) 重写方法内调用它

private void createShortCut() {

     Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
     shortcutIntent.setAction(Intent.ACTION_MAIN);
     Intent intent = new Intent();
     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, R.string.app_name);
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
     intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
     getApplicationContext().sendBroadcast(intent);
}

最后创建 bool 变量,在调用上述方法之前将其存储在共享首选项中,确保 bool 变量为 false,这只是为了避免多个快捷方式。 不要忘记在 list 中添加权限 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 希望对您有帮助

关于android - 安装应用程序时如何自动将应用程序启动器图标添加到主屏幕(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26612287/

相关文章:

res(generated) 文件夹下的 Android Security : values. xml

Android: java.lang.IllegalStateException: 指定的 child 已经有一个 parent 。您必须首先对 child 的 parent 调用 removeView()。

android - Lotus Notes Traveler 应用程序中的 Intent.ACTION_SEND 问题

android - 在 Kitkat 上没有 LAUNCHER 时的 BroadcastReceiver

android - 在不中断/停止Android中的背景音频的情况下播放视频

android - 在申请中保留包含条款的文件

android - 如何在一个应用程序中设置两个不同的启动器名称和图标 (android)

SDK 升级后 Android 模拟器运行缓慢且出现故障

android - 自动阻止应用程序从启动器启动

安卓 USB 设备 : how to determine "vendor name" and "device name" from vendorID and DeviceID?