首页 > 代码库 > 判断快捷方式是否存在以及创建

判断快捷方式是否存在以及创建

	/**	 * 判断指定快捷方式是否存在(对root手机有效)	 * 	 * @param context	 *            上下文对象	 * @param title	 *            标题	 * @return 存在返回true,不存在或者不具有root权限返回false	 */	public static boolean isExistShortCut(Context context, String title) {		try {			String authority = PermissionUtil.getAuthorityFromPermission(					context, Constant.READ_SETTINGS);			if (DataUtil.isEmpty(authority)) {				return false;			}			String url = "content://" + authority + "/favorites?notify=true";			ContentResolver resolver = context.getContentResolver();			Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",					new String[] { title }, null);			while (cursor != null && cursor.moveToNext()) {				String intent = cursor.getString(cursor						.getColumnIndex("intent"));				if (intent != null && intent.contains(context.getPackageName())) {					cursor.close();					return true;				}			}			if (cursor != null) {				cursor.close();			}			return false;		} catch (Exception e) {			e.printStackTrace();		}		return false;	}

/**	 * 创建桌面快捷方式	 * 	 * @param context	 *            上下文对象	 * @param title	 *            标题	 * @param intent	 *            点击触发的Intent	 * @param icon	 *            图标	 */	public static void createShortCut(Context context, String title,			Intent intent, Bitmap icon) {		System.out.println(intent);		try {			Intent shortIntent = new Intent(Constant.INSTALL_SHORTCUT);			shortIntent.putExtra("duplicate", false);			shortIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);			if (icon != null) {				float size = context.getResources().getDimension(						android.R.dimen.app_icon_size);				shortIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON,						BitmapUtil.zoomBitmap(icon, size, size));			}			shortIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);			if (!isExistShortCut(context, title)) {				context.sendBroadcast(shortIntent);			}		} catch (Exception e) {			e.printStackTrace();		}	}


 

/**	 * 根据权限获取其对应的系统类全路径(部分权限需要手机具有root权限,否则返回null)	 * 	 * @param context	 *            上下文对象	 * @param permission	 *            权限	 * @return 对应权限的系统类全路径	 */	public static String getAuthorityFromPermission(Context context,			String permission) {		List<PackageInfo> packageInfos = context.getPackageManager()				.getInstalledPackages(PackageManager.GET_PROVIDERS);		if (DataUtil.isEmpty(packageInfos)) {			return null;		}		for (PackageInfo pack : packageInfos) {			ProviderInfo[] providers = pack.providers;			if (providers == null || providers.length <= 0) {				continue;			}			for (ProviderInfo provider : providers) {				if (permission.equals(provider.readPermission))					return provider.authority;				if (permission.equals(provider.writePermission))					return provider.authority;			}		}		return null;	}


 

判断快捷方式是否存在以及创建