android桌面快捷方式的创建与删除

/**
	 * 创建多个桌面快捷方式
	 * 
	 * @param list
	 */
	private void createShortCut(String[] list) {
		// 安装的Intent
		Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

		for (int i = 0; i < list.length; i++) {
			String tName = list[i];
			if (isCreateShortCut(tName)) {
				System.out.println("桌面上已经有快捷方式:" + tName + " 不再创建.");
				continue;
			}
			// 快捷名称
			shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, tName);
			// 快捷图标是允许重复
			shortcut.putExtra("duplicate", false);

			// 添加 action
			Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
			shortcutIntent.putExtra("tName", tName);
			// 必须设置为注册过上面 action 的类
			String appClass = this.getPackageName() + "." + this.getLocalClassName();
			shortcutIntent.setClassName(this.getPackageName(), appClass);
			shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
			
			intentList[i] = shortcutIntent;

			// 快捷图标
			ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
			shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

			// 发送广播
			sendBroadcast(shortcut);
		}
	}

	/**
	 * 删除多个桌面快捷方式
	 * @param list
	 */
	private void delShortcut(String[] list) {
		// 删除的Intent
		Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

		for (int i = 0; i < list.length; i++) {
			// 快捷方式的名称
			shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, list[i]);
			
			// 添加 action
			Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
			shortcutIntent.putExtra("tName", list[i]);
			// 必须设置为注册过上面 action 的类
			String appClass = this.getPackageName() + "." + this.getLocalClassName();
			shortcutIntent.setClassName(this.getPackageName(), appClass);
			shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
			
			sendBroadcast(shortcut);
		}

	}

	/**
	 * 判断name这个快捷方式是否已经创建
	 * 
	 * @param name
	 * @return
	 */
	private boolean isCreateShortCut(String name) {
		boolean isInstallShortcut = false;
		String[] permission = { "com.android.launcher.permission.WRITE_SETTINGS", "com.android.launcher.permission.READ_SETTINGS" };
		String AUTHORITY = null;
		// 获取当前应用名称
		// try {
		// final PackageManager pm = getPackageManager();
		// title =
		// pm.getApplicationLabel(pm.getApplicationInfo(getPackageName(),
		// PackageManager.GET_META_DATA)).toString();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

		// 其实快捷方式信息是保存在 AUTHORITY=com.android.launcher的launcher.db的favorites表中
		// android默认的 AUTHORITY 在2.2以后是
		// com.android.launcher2.settings,但是不同的厂商可能会做不同的修改,所以废弃,在后面代码中进行查询
		// if (android.os.Build.VERSION.SDK_INT < 8) {
		// AUTHORITY = "com.android.launcher.settings";
		// }else{
		// AUTHORITY = "com.android.launcher2.settings";
		// }
		// 由于不同的厂商uri的前缀不同,所以,我们需要去查询provider获取真实的content的uri前缀
		for (int i = 0; i < permission.length; i++) {
			if ((AUTHORITY = getAuthorityFromPermission(this, permission[i])) != null) {
				System.out.println("AUTHORITY: " + AUTHORITY);
				break;
			}
		}
		final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");

		Cursor c = getContentResolver().query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { name }, null);// XXX表示应用名称。
		if (c != null && c.getCount() > 0) {
			isInstallShortcut = true;
			c.close();
		} else if(c != null){
			c.close();
		}
		return isInstallShortcut;
	}

	/**
	 * 根据指定的permission权限查询authority
	 * 
	 * @param context
	 * @param permission
	 * @return
	 */
	private String getAuthorityFromPermission(Context context, String permission) {
		if (permission == null) {
			return null;
		}
		List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
		if (packs != null) {
			for (PackageInfo pack : packs) {
				ProviderInfo[] providers = pack.providers;
				if (providers != null) {
					for (ProviderInfo provider : providers) {
						if (permission.equals(provider.readPermission)) {
							return provider.authority;
						}
						if (permission.equals(provider.writePermission)) {
							return provider.authority;
						}
					}
				}
			}
		}
		return null;
	}

需要的权限;

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


部分手机出现删除快捷方式权限拒绝:

05-17 16:41:25.840: W/BroadcastQueue(355): Permission Denial: broadcasting Intent { act=com.android.launcher.action.UNINSTALL_SHORTCUT flg=0x10 (has extras) } from com.example.shortcut (pid=7905, uid=10051) requires com.android.launcher.permission.UNINSTALL_SHORTCUT due to receiver com.shendu.launcher/com.shendu.launcher.UninstallShortcutReceiver

添加权限:

<permission android:label="@string/app_name" android:name="com.android.launcher.permission.INSTALL_SHORTCUT" android:protectionLevel="normal" android:permissionGroup="android.permission-group.SYSTEM_TOOLS" />
    <permission android:label="@string/app_name" android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" android:protectionLevel="normal" android:permissionGroup="android.permission-group.SYSTEM_TOOLS" />


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值