我创建了一个 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/