【问题标题】:Android: Get more icons (shortcuts) to activities in my package on the homescreenAndroid:在主屏幕上获取我的包中的活动的更多图标(快捷方式)
【发布时间】:2013-01-15 05:05:06
【问题描述】:
我知道我可以创建一个可以放置在主屏幕上的小部件,但是当用户安装应用程序时,是否有可能只有我的标准启动器图标会启动某个活动。但是当用户选择时(例如通过单击我的应用程序中的按钮)会在设备的主屏幕上创建另一个图标,直接链接到另一个活动?因此,通过单击主屏幕上的该图标,我的包中的另一个活动将打开?
如果可能的话,有人有sn-p吗?
谢谢!
【问题讨论】:
标签:
android
launcher
homescreen
android-launcher
app-launcher
【解决方案1】:
感谢这个博客:http://viralpatel.net/blogs/android-install-uninstall-shortcut-example/
在 Manifest 中添加所需的权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
在快捷方式所指的清单中添加您的活动:
android:exported="true"
然后使用以下方法安装/卸载快捷方式:
private void addShortcut() {
//Adding shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
private void removeShortcut() {
//Deleting shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent
.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
要将活动添加到快捷方式菜单,只需将此意图过滤器添加到清单中的活动:
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>