"Android" to determine the existence of a shortcut to the principle--optimization of the online universal (10 times times faster)

Source: Internet
Author: User

1 principle: This is in 2.3 when the unintentional discovery, when I follow the example on the Internet to find out whether the shortcut is created when suddenly the application hangs, said to be a lack of permissions.

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

Then I added permissions, although it can be judged, but Xiaomi, HTC and other mobile phones can not be used, at this time suddenly found a piece of code on the Internet, only to understand the ideas of netizens, now to simple analysis, and make optimization.

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

In fact, the judgment shortcut is based on this contentprovider to judge. But we don't know about this authority, so how do we know? Query, according to Packagemanager get Providerinfo, and then determine whether the Providerinfo read and Write permission contains com.android.launcher.permission.READ_ SETTINGS or Com.android.launcher.permission.WRITE_SETTINGS.

We have understood this principle.

2 Solution: 1 This is the general wording of the Web:
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! = Nu ll && 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 (Pro Viderinfo 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 way of thinking, although the idea came together, but a little bit in and out:
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! = Nu ll && 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 , the 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" to determine the existence of a shortcut to the principle--optimization of the online universal (10 times times faster)

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.