編輯:關於Android編程
前兩天做了個應用,需要實時獲取桌面快捷方式是否存在,在某些第三方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 { Listpacks = 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裡添加所有權限
Volley是Google I/O 2013推出的網絡通信庫,在volley推出之前我們一般會選擇比較成熟的第三方網絡通信庫,如:android-async-httpre
酷狗音樂最新版有ktv模式啦,大家想k歌的話不用去ktv也可以自已在家k歌,愛唱歌的朋友一起來學習下酷狗音樂怎麼k歌吧! 1.首先,打開酷狗這個軟件,建議
ViewStub可以在運行時動態的添加布局。幫助文檔給定的定義是:A ViewStub is an invisible, zero-sized View that can
hibernate是非常典型的持久層框架,持久化的思想是非常值得我們學習和研究的。這篇博文,我們主要以實例的形式學習Hibernate,不深究Hibernate的思想和原