码迷,mamicode.com
首页 > 移动开发 > 详细

【Android】判断快捷方式是否存在的原理——优化网上通用的(速度提升10倍)

时间:2015-05-29 10:00:16      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

1原理:

这个是在2.3的时候无意发现的,当我照着网上的例子查询快捷方式是否创建的时候突然应用程序挂了,说是缺少权限。

com.android.launcher.permission.READ_SETTINGS或者com.android.launcher.permission.WRITE_SETTINGS

然后我就添加了权限,虽然是可以判断了,但是小米,HTC等手机都不能正常使用,这时候突然发现了网上了一段代码,才明白了网友们的想法,现在就来简单分析下,并做出优化。

String url = "content://" + authority + "/favorites?notify=true";

其实判断快捷方式也就是根据这个ContentProvider来判断。但是我们并不知道这个authority,那么怎么知道呢?查询,根据PackageManager得到ProviderInfo,然后判断ProviderInfo的读写权限是否包含com.android.launcher.permission.READ_SETTINGS或者com.android.launcher.permission.WRITE_SETTINGS。

至此原理我们已经明白了。

2解决方案:

1这是网上通用的写法:

	public static boolean hasShortcut(Context context, String appName) {
		String readSettingsPermission = "com.android.launcher.permission.READ_SETTINGS";
		String authority = getAuthorityFromPermission2(context, readSettingsPermission);
		String url = "content://" + authority + "/favorites?notify=true";
		final Uri CONTENT_URI = Uri.parse(url);
		Cursor c = context.getContentResolver().query(CONTENT_URI, null, " title= ? ", new String[] { appName }, null);
		if (c != null && c.moveToNext()) {
			return true;
		}
		return false;
	}
	
	@SuppressLint("NewApi")
	private static String getAuthorityFromPermission2(Context context, String permission) {
		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;
	}

2这是我自己想的方法,虽然思路走到一起了,但是多少有点出入:

	public static boolean hasShortcut(Context context, String appName) {
		String readSettingsPermission = "com.android.launcher.permission.READ_SETTINGS";
		String authority = getAuthorityFromPermission1(context, readSettingsPermission);
		String url = "content://" + authority + "/favorites?notify=true";
		final Uri CONTENT_URI = Uri.parse(url);
		Cursor c = context.getContentResolver().query(CONTENT_URI, null, " title= ? ", new String[] { appName }, null);
		if (c != null && c.moveToNext()) {
			return true;
		}
		return false;
	}

	private static String getAuthorityFromPermission1(Context context, String permission) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> appProcessInfos = am.getRunningAppProcesses();
		for (int i = 0; i < appProcessInfos.size(); i++) {
			RunningAppProcessInfo appInfo = appProcessInfos.get(i);
			List<ProviderInfo> info = context.getPackageManager().queryContentProviders(appInfo.processName, appInfo.uid, PackageManager.GET_PROVIDERS);
			if (info != null) {
				for (int j = 0; j < info.size(); j++) {
					ProviderInfo provider = info.get(j);
					if (permission.equals(provider.readPermission)) {
						return provider.authority;
					}
					if (permission.equals(provider.writePermission)) {
						return provider.authority;
					}
				}
			}
		}
		return null;
	}


【Android】判断快捷方式是否存在的原理——优化网上通用的(速度提升10倍)

标签:

原文地址:http://blog.csdn.net/windowsxp2014/article/details/46138053

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!