1 、创建

view plain copy to clipboard print ?
  1. /**
  2. *为程序创建桌面快捷方式
  3. */
  4. privatevoidaddShortcut(){
  5. Intentshortcut=newIntent("com.android.launcher.action.INSTALL_SHORTCUT");
  6. //快捷方式的名称
  7. shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
  8. shortcut.putExtra("duplicate",false);//不允许重复创建
  9. //指定当前的Activity为快捷方式启动的对象:如com.everest.video.VideoPlayer
  10. //注意:ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
  11. ComponentNamecomp=newComponentName(this.getPackageName(),"."+this.getLocalClassName());
  12. shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(Intent.ACTION_MAIN).setComponent(comp));
  13. //快捷方式的图标
  14. ShortcutIconResourceiconRes=Intent.ShortcutIconResource.fromContext(this,R.drawable.icon);
  15. shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconRes);
  16. sendBroadcast(shortcut);
  17. }

2、删除

view plain copy to clipboard print ?
  1. /**
  2. *删除程序的快捷方式
  3. */
  4. privatevoiddelShortcut(){
  5. Intentshortcut=newIntent("com.android.launcher.action.UNINSTALL_SHORTCUT");
  6. //快捷方式的名称
  7. shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
  8. //指定当前的Activity为快捷方式启动的对象:如com.everest.video.VideoPlayer
  9. //注意:ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
  10. StringappClass=this.getPackageName()+"."+this.getLocalClassName();
  11. ComponentNamecomp=newComponentName(this.getPackageName(),appClass);
  12. shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent(Intent.ACTION_MAIN).setComponent(comp));
  13. sendBroadcast(shortcut);
  14. }

3、声明权限

在AndroidManifest.xml 文件中声明 创建和删除快捷方式时声明权限

view plain copy to clipboard print ?
  1. <uses-permissionandroid:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
  2. <uses-permissionandroid:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>

更多相关文章

  1. android Notification的使用
  2. Android创建XMl文件
  3. Android对Window对象的管理机制分析
  4. Android(安卓)图片缩放
  5. Android(安卓)ContentObserver ---拦截短信并删除
  6. Android创建快捷方式(shortcut)
  7. android 开发 实例 下部主导航(1)
  8. Android桌面快捷方式的实现
  9. Android(安卓)虚拟机安装SD卡

随机推荐

  1. 使用limit,offset分页场景时为什么会慢
  2. MySQL控制用户输错密码尝试次数
  3. mysql命令行脚本执行操作示例
  4. MySQL查询语句简单操作示例
  5. MySQL查询排序与查询聚合函数用法分析
  6. 关于MySQL索引的深入解析
  7. MySQL查询条件常见用法详解
  8. 浅析CentOS6.8安装MySQL8.0.18的教程(RPM
  9. SQL面试题:求时间差之和(有重复不计)
  10. CentOS7.6安装MYSQL8.0的步骤详解