Android中创建快捷方式代码实例
1、添加权限(必须)
<uses-permissionandroid:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
2、添加快捷键
publicstaticvoidsetupShortcut(Activityactivity) { IntentshortcutIntent=newIntent(activity,MainActivity.class);//启动首页(launcherActivity)
Intentintent=newIntent("com.android.launcher.action.INSTALL_SHORTCUT"); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"hello");//快捷键名字可以任意,不过最好为app名称 ParcelableiconResource=Intent.ShortcutIconResource.fromContext(activity,R.drawable.ic_launcher); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconResource); intent.putExtra("duplicate",false);//不允许重复创建
activity.sendBroadcast(intent);//发送广播创建快捷键 }