如何在安装应用程序时自动添加应用程序启动器图标到主屏幕(安卓)
问题描述:
我创建了一个Android应用程序。而我想要的是当我安装我的应用程序时,它应该自动添加快捷键/启动器图标到主屏幕。如何在安装应用程序时自动添加应用程序启动器图标到主屏幕(安卓)
当我安装我的应用程序时,一个启动器图标应自动创建。
我试过这个:但它不工作。
<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);
点击此处了解详情:Add Shortcut for android application To home screen On button click
答
藤做到这一点,你需要做反向的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);
}
最后在调用之前创建布尔变量将其存储在共享首选项中Above方法确保布尔变量为false这只是为了避免多个快捷方式。 不要忘记添加许可,您的清单<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
希望它可以帮助你
的可能重复[我怎样才能把发射器的主屏幕上的应用程序图标?(http://stackoverflow.com/questions/4854197/how -can-i-place-app-icon-on-launcher-home-screen) – 2014-11-03 17:33:15
要在启动器屏幕上显示一个典型的图标,除了声明通常的意图过滤器外,通常不需要执行任何操作**清单中的启动器活动。 ' intent-filter> –
2014-11-03 17:43:43