Android创建快捷方式,Android检查快捷方式,Android删除快捷方式

我直接提供一个类出来吧,可以直接copy使用

/** * @author YOLANDA * @Time 2015年3月14日 下午4:04:35 */public class ShortCutUtils {private Context context;public ShortCutUtils(Context context) {this.context = context;}/** * 创建快捷方式 * @author YOLANDA */public void createShortCut() {// <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));// 不允许重复创建 一些实体机上无效,如三星 galaxy noteintent.putExtra("duplicate", false);Intent sIntent = new Intent(Intent.ACTION_MAIN);sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载sIntent.setClass(context, WelActivity.class);intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sIntent);context.sendBroadcast(intent);}/** * 检查快捷方式 * @author YOLANDA * @return */public boolean hasShortCut() {// 测试发现,模拟器和moto平板M604可以正常判断。而三星I9000,HTC G6,三星GALAXY NOTE无法判断/*<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>*/String url = "";if (Build.VERSION.SDK_INT < 8) {url = "content://com.android.launcher.settings/favorites?notify=true";} else {url = "content://com.android.launcher2.settings/favorites?notify=true";}ContentResolver resolver = context.getContentResolver();Cursor cursor = resolver.query(Uri.parse(url), null, "title=?", new String[] { context.getString(R.string.app_name) }, null);if (cursor != null && cursor.moveToFirst()) {cursor.close();return true;}return false;}/** * 卸载快捷方式 * @author YOLANDA */public void delShortcut() {/*<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />*/Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");// 快捷方式的名称shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer// 注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式Intent sIntent = new Intent(Intent.ACTION_MAIN);sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载sIntent.setClass(context, MainActivity.class);shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, sIntent);context.sendBroadcast(shortcut);}}


更多相关文章

  1. Android百度地图开发(四):创建地图
  2. Android的设计模式-建造者模式
  3. Kubernetes 使用kubeadm创建集群
  4. Ubuntu20.04服务器+Anaconda上创建Python3.6虚拟环境并安装Pytor
  5. python中if和elif的区别介绍
  6. md5加密与数组函数
  7. 创建简历表格和注册表单
  8. 创建table表格和创建form表单并JS判断
  9. 创建html和常用标签

随机推荐

  1. [Gradle] Gradle 构建 android 应用常见
  2. 好的android 相关的技术博客
  3. 菜单Menu
  4. Android(安卓)Studio第十五期 - 友盟统计
  5. Android(安卓)PreferenceActivity与Prefe
  6. Android(安卓)同时播放缩放动画和位移动
  7. 开源中国WP7客户端全面开源,包括iPhone客
  8. Android使用的MQTT客户端
  9. Android(安卓)ListFragment自定义view报
  10. Android中的Handler、Looper、Message简