如何判断程序来自快捷方式 android
1个回答
展开全部
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询