前两天做了个应用,需要实时获取桌面快捷方式是否存在,在某些第三方ROM下无法获取。
网上大量的例子都是谷歌原生系统或者小米、三星这类系统起作用,但是对于第三方ROM无法获取如:HTC、华为、一加、联想。
为什么不可以,试过的同学应该都知道,我这里就不一一解答,直接贴正确代码:
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 launcherUtil { private static String AUTHORITY = null; public static boolean isShortCutExist(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表示应用名称。 if (c != null && c.getCount() > 0) { isInstallShortcut = true; } if (null != c && !c.isClosed()) c.close(); } catch (Exception e) { // TODO: handle exception Log.e("isShortCutExist", 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.activityInfo == null) { // should 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<PackageInfo> 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)// 精准匹配launcher.settings,再一次验证 && (provider.authority) .contains(".launcher.settings")) return provider.authority; } } } } } catch (Exception e) { e.printStackTrace(); } return ""; } public static String getAuthorityFromPermission(Context context) { // 获取默认 String authority = getAuthorityFromPermissionDefault(context); // 获取特殊第三方 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)以及以下的 authority = "com.android.launcher.settings"; } else if (sdkInt < 19) {// Android 4.4以下 authority = "com.android.launcher2.settings"; } else {// 4.4以及以上 authority = "com.android.launcher3.settings"; } } authority = "content://" + authority + "/favorites?notify=true"; return authority; } }
最后别忘了XML里添加所有权限
<!-- 快捷方式相关 --> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.android.launcher2.permission.READ_SETTINGS" /> <uses-permission android:name="com.android.launcher2.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS" /> <uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS" /> <uses-permission android:name="org.adw.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="org.adw.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.htc.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.qihoo360.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.qihoo360.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.lge.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.lge.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="net.qihoo.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="net.qihoo.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="org.adwfreak.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="org.adwfreak.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="org.adw.launcher_donut.permission.READ_SETTINGS" /> <uses-permission android:name="org.adw.launcher_donut.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.huawei.launcher3.permission.READ_SETTINGS" /> <uses-permission android:name="com.huawei.launcher3.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.fede.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.fede.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.sec.android.app.twlauncher.settings.READ_SETTINGS" /> <uses-permission android:name="com.sec.android.app.twlauncher.settings.WRITE_SETTINGS" /> <uses-permission android:name="com.anddoes.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.anddoes.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.tencent.qqlauncher.permission.READ_SETTINGS" /> <uses-permission android:name="com.tencent.qqlauncher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.huawei.launcher2.permission.READ_SETTINGS" /> <uses-permission android:name="com.huawei.launcher2.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.android.mylauncher.permission.READ_SETTINGS" /> <uses-permission android:name="com.android.mylauncher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.ebproductions.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.ebproductions.android.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.oppo.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.oppo.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.lenovo.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.lenovo.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS" /> <uses-permission android:name="telecom.mdesk.permission.READ_SETTINGS" /> <uses-permission android:name="telecom.mdesk.permission.WRITE_SETTINGS" /> <uses-permission android:name="dianxin.permission.ACCESS_LAUNCHER_DATA" />
时间: 2024-11-07 10:05:22