[Android] determines whether shortcuts exist-more compatible (12 times faster) and android12times faster

Source: Internet
Author: User

[Android] determines whether shortcuts exist-more compatible (12 times faster) and android12times faster
1 principle: I did not intend to find this in 2.3. When I followed the online example to check whether the shortcut was created, the application suddenly crashed, saying that the application lacked permissions.

Com. android. launcher. permission. READ_SETTINGS or com. android. launcher. permission. WRITE_SETTINGS

Then I added the permission. Although the permission can be determined, , HTC and other mobile phones cannot be used normally. At this time, I suddenly found a piece of code on the Internet to understand the ideas of netizens, now let's take a simple analysis and make optimization.


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

In fact, the judgment shortcut is determined based on this ContentProvider. But we don't know this authority. How can we know it? Query, obtain ProviderInfo Based on PackageManager, and then determine whether the read and write permissions of ProviderInfo include com. android. launcher. permission. READ_SETTINGS or com. android. launcher. permission. WRITE_SETTINGS.

So far, we have understood the principle.

2 solutions: 1. This is a common online statement:
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. This is my own method. Although the idea has come together, it is somewhat different:
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;}
3. This is the last version. The speed is improved by 20% and the compatibility is stronger. Remember to add the permission.
Public static boolean hasShortcut (Context context, String appName) {long start = System. currentTimeMillis (); String authority = getAuthorityFromPermission (context); if (authority = null) {return false;} long end = System. currentTimeMillis ()-start; Log. e ("Finals", end + "eee"); String url = "content: //" + authority + "/favorites? Required y = true "; try {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;} catch (Exception e) {} return false;} private static String getAuthorityFromPermission (Context context) {// obtain the default LauncherIntent intent = new Intent (Intent. ACTION_MAIN); intent. addCategory (Intent. CATEGORY_HOME); PackageManager mPackageManager = context. getPackageManager (); ResolveInfo resolveInfo = mPackageManager. resolveActivity (intent, 0); if (resolveInfo = nu Ll) {return null;} List <ProviderInfo> info = response. queryContentProviders (resolveInfo. activityInfo. packageName, resolveInfo. activityInfo. applicationInfo. uid, PackageManager. GET_PROVIDERS); if (info! = Null) {for (int j = 0; j <info. size (); j ++) {ProviderInfo provider = info. get (j); if (provider. readPermission = null) {continue;} if (Pattern. matches (". * launcher. * READ_SETTINGS ", provider. readPermission) {return provider. authority ;}} return null ;}

This is a common desktop permission:

<! -- Shortcut permission -->

<Uses-permissionandroid: name = "com. android. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. android. launcher2.permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. android. launcher3.permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "org. adw. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. htc. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. qihoo360.launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. lge. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "net. qihoo. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "org. adwfreak. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "org. adw. launcher_donut.permission.READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. huawei. launcher3.permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. fede. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. sec. android. app. twlauncher. settings. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. anddoes. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. tencent. qqlauncher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. huawei. launcher2.permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. android. mylauncher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. ebproductions. android. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. oppo. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. lenovo. launcher. permission. READ_SETTINGS"/>

<Uses-permissionandroid: name = "com. huawei. android. launcher. permission. READ_SETTINGS"/>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.