标签:
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。
至此原理我们已经明白了。
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; }
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