Android determines whether Desktop shortcuts exist
An application was created two days ago. You need to obtain the existence of Desktop shortcuts in real time, which cannot be obtained in some third-party ROM.
A large number of examples on the Internet are Google's native systems or systems such as Xiaomi and Samsung, but they cannot be obtained from third-party ROM systems such as HTC, Huawei, Yijia, and Lenovo.
Why can't I do it? All the people who have tried it should know that I will not answer them one by one here. I will directly paste the correct code:
Import java. util. list; import android. content. contentResolver; import android. content. context; import android. content. intent; import android. content. pm. packageInfo; import android. content. pm. packageManager; import android. content. pm. providerInfo; import android. content. pm. resolveInfo; import android. database. cursor; import android.net. uri; import android. text. textUtils; import android. util. log; public class la UncherUtil {private static String AUTHORITY = null; public static boolean isw.cutexist (Context context, String title) {boolean isInstallShortcut = false; if (null = context | TextUtils. isEmpty (title) return isInstallShortcut; if (TextUtils. isEmpty (AUTHORITY) AUTHORITY = getAuthorityFromPermission (context); final ContentResolver cr = context. getContentResolver (); if (! TextUtils. isEmpty (AUTHORITY) {try {final Uri CONTENT_URI = Uri. parse (AUTHORITY); Cursor c = cr. query (CONTENT_URI, new String [] {"title", "iconResource"}, "title =? ", New String [] {title}, null); // XXX indicates the application name. If (c! = Null & c. getCount ()> 0) {isInstallShortcut = true;} if (null! = C &&! C. isClosed () c. close ();} catch (Exception e) {// TODO: handle exceptionLog. e ("isw.cutexist", e. getMessage () ;}return isInstallShortcut;} public static String getCurrentLauncherPackageName (Context context) {Intent intent = new Intent (Intent. ACTION_MAIN); intent. addCategory (Intent. CATEGORY_HOME); ResolveInfo res = context. getPackageManager (). resolveActivity (intent, 0); if (res = null | res. activity Info = null) {// shocould not happen. A home is always installed, isn' t it? Return "";} if (res. activityInfo. packageName. equals ("android") {return "";} else {return res. activityInfo. packageName ;}} public static String getAuthorityFromPermissionDefault (Context context) {return getThirdAuthorityFromPermission (context, "com. android. launcher. permission. READ_SETTINGS ");} public static String getThirdAuthorityFromPermission (Context context, String permission) {if (TextUtils. isEmpty (permission) {return "";} try {List
Packs = context. getPackageManager (). getInstalledPackages (PackageManager. GET_PROVIDERS); if (packs = null) {return "";} for (PackageInfo pack: packs) {ProviderInfo [] providers = pack. providers; if (providers! = Null) {for (ProviderInfo provider: providers) {if (permission. equals (provider. readPermission) | permission. equals (provider. writePermission) {if (! TextUtils. isEmpty (provider. authority) // precisely matches launcher. settings, verify again & (provider. authority ). contains (". launcher. settings ") return provider. authority ;}}} catch (Exception e) {e. printStackTrace ();} return "";} public static String getAuthorityFromPermission (Context context) {// obtain the default String authority = getAuthorityFromPermissionDefault (context ); // obtain the special third-party if (authority = null | authority. trim (). equals ("") {String packageName = getCurrentLauncherPackageName (context); packageName + = ". permission. READ_SETTINGS "; authority = getThirdAuthorityFromPermission (context, packageName);} // if (TextUtils. isEmpty (authority) {int sdkInt = android. OS. build. VERSION. SDK_INT; if (sdkInt <8) {// Android 2.1.x (API 7) and the following authority = "com. android. launcher. settings ";} else if (sdkInt <19) {// Android 4.4 The following authority = "com. android. launcher2.settings ";} else {// 4.4 and above authority =" com. android. launcher3.settings ";}} authority =" content: // "+ authority +"/favorites? Required y = true "; return authority ;}}
Do not forget to add all permissions in XML.