Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android判斷桌面快捷方式是否存在

Android判斷桌面快捷方式是否存在

編輯:關於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 {
			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)// 精准匹配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裡添加所有權限

 


    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved