繁体   English   中英

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

[英]how to automatically add application launcher icon to home screen when app is installed (android)

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

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

我试过了:但是没有用。

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

要执行此操作,您需要反向操作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);
}

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

您需要发送广播:

//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);

此处的更多信息: 将android应用程序的快捷方式添加到主屏幕在按钮上单击

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM