1.创建快捷方式

/** * 为程序创建桌面快捷方式 */ private void addShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); shortcut.putExtra("duplicate", false); //不允许重复创建 //指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer //注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序 ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); //快捷方式的图标 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); sendBroadcast(shortcut); }

2.删除快捷方式

/** * 删除程序的快捷方式 */ private void delShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); //快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); //指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer //注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式 String appClass = this.getPackageName() + "." +this.getLocalClassName(); ComponentName comp = new ComponentName(this.getPackageName(), appClass); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); sendBroadcast(shortcut); }

3.声明权限

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

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

更多相关文章

  1. Android通过内容提供器获取相册中所有图片
  2. Activity半透明效果
  3. android 倒影
  4. Android(安卓)PopulWindow创建与定位
  5. android java 执行shell命令(笔记)
  6. Android(安卓)HandlerThread总结
  7. android 由entity创建创建表的sql语句
  8. C#开发android之alertdialog用法
  9. Android(安卓)如何删除短信

随机推荐

  1. 【Android】Http请求
  2. android 根据EditText 内容,改变TextView
  3. android WebView结合javascript相互调用
  4. [Android]获取网络连接状态
  5. 像素鸟(flapybird)的简单实现
  6. Android(安卓)可選文件格式瀏覽器
  7. Android(Java):长时间未登录提醒
  8. android截屏简单引用
  9. Android(安卓)之 SeekBar用法介绍
  10. Android(安卓)获取设备宽高分辨率