桌面快捷键——创建,删除,检测

本文尝试实现QQ桌面快捷键功能,当有消息时显示消息数目,但作者因能力有限未能完成。提供了创建桌面快捷键的代码,并讨论了所需的权限。文章最后邀请读者提供反馈。

本来想实现一下QQ这个牛X的桌面快捷键,有消息的时候会显示消息数目的功能的,结果小弟实在能力有限,实现不了啊,只能等以后在实现了。

桌面快捷键的创建很简单,就几行代码,大家用的话直接copy走就哦了,废话不多说了,看代码

       /**
	 * 判断快捷键是否已经存在
	 * 
	 * @param context
	 *                上下文
	 * @param iconName
	 *                快捷键的名称
	 * @return 如果存在返回true,不存在返回发fasle
	 */
	public static boolean hasShortcut(Context context, String iconName) {
		final String AUTHORITY = getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS");
		final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
		Cursor c = context.getContentResolver().query(CONTENT_URI, new String[] { "title" }, "title=?", new String[] { iconName }, null);
		if (c != null && c.moveToNext()) {
			return true;
		}
		return false;
	}

       /**
	 * 判断快捷键是否已经存在
	 * 
	 * @param context
	 *                上下文
	 * @param iconName
	 *                快捷键的名称
	 * @return 如果存在返回true,不存在返回发fasle
	 */
	public static boolean hasShortcut(Context context, String iconName) {
		final String AUTHORITY = getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS");
		final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
		Cursor c = context.getContentResolver().query(CONTENT_URI, new String[] { "title" }, "title=?", new String[] { iconName }, null);
		if (c != null && c.moveToNext()) {
			return true;
		}
		return false;
	}

	/**
	 * 获取系统共享数据
	 */
	private static String getAuthorityFromPermission(Context context, String permission) {
		if (TextUtils.isEmpty(permission)) {
			return null;
		}
		List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
		if (packs == null) {
			return null;
		}
		for (PackageInfo pack : packs) {
			ProviderInfo[] providers = pack.providers;
			if (providers != null) {
				for (ProviderInfo provider : providers) {
					if (permission.equals(provider.readPermission) || permission.equals(provider.writePermission)) {
						return provider.authority;
					}
				}
			}
		}
		return null;
	}

       /**
	 * 创建桌面快捷方式
	 */
	public static void createDesktopIcon(Context context) {
		final Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		// 不允许重复创建
		addIntent.putExtra("duplicate", false);
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));// 快捷方式的标题
		// 快捷方式的图标
		final Parcelable icon = paintIcon(context); // 获取快捷键的图标
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);// 快捷方式的图标
		// 绑定快捷方式和应用程序
		final Intent myIntent = new Intent(context, MainActivity.class);
		myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		myIntent.setAction("android.intent.action.MAIN");
		myIntent.addCategory("android.intent.category.LAUNCHER");
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的动作
		context.sendBroadcast(addIntent);
	}

	/**
	 * 获取快捷方式的图标
	 */
	private static Bitmap paintIcon(Context context) {
		// 初始化画布
		int iconSize = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
		Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);
		Canvas canvas = new Canvas(contactIcon);
		// 获取系统的ic_launcher作为快捷键图标
		Bitmap icon = ((BitmapDrawable) (context.getResources().getDrawable(R.drawable.ic_launcher))).getBitmap();
		// 拷贝图片
		Paint iconPaint = new Paint();
		iconPaint.setDither(true);// 防抖动
		iconPaint.setFilterBitmap(true);// 用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果
		Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
		Rect dst = new Rect(0, 0, iconSize, iconSize);
		canvas.drawBitmap(icon, src, dst, iconPaint);
		return contactIcon;
	}

其中三个操作分别用到三个权限:

    <!-- 检测图标是否已经存在,获取系统共享数据的权限 -->
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <!-- 创建快捷键的权限 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <!-- 删除快捷键的权限 -->
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
哪个大神看了以后觉得对QQ快捷键上的消息有看法的话,请告诉小弟吧!不胜感激啊!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值