我创建了一个Android应用程序。我想要的是当我安装我的应用程序时,它应该自动将快捷方式/启动器图标添加到主屏幕。
当我安装我的应用程序时,应自动创建启动器图标。
我试过这个:但它没有用。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
答案 0 :(得分:4)
Todo这样做你需要做反向Android,因为主屏幕在用户控制下,但仍然有办法 只需在oncreate方法之外创建一个方法createShortCut()并在onCreate(Bundle savedInstanceState)覆盖方法中调用它
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);
}
最后创建布尔变量在调用之前将它存储在共享首选项中。上面的方法确保布尔变量为false这只是为了避免多个快捷方式。
别忘了在你的清单<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
中添加权限
希望它能帮到你
答案 1 :(得分:1)
您需要发送广播:
//where this is a context (e.g. your current activity)
final Intent shortcutIntent = new Intent(this, SomeActivity.class);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
此处提供更多信息:Add Shortcut for android application To home screen On button click