安卓判断快捷方式是否存在
2018-07-20 来源:open-open
public static boolean isAddShortCut(Context context) {
boolean isInstallShortcut = false;
final ContentResolver cr = context.getContentResolver();
String AUTHORITY = getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS");
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI, new String[] {"title"}, "title=?", new String[] {context.getString(R.string.app_name)}, null);
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
}
if (c != null) {
c.close();
}
return isInstallShortcut;
}
public static String getAuthorityFromPermission(Context context, String permission) {
if (TextUtils.isEmpty(permission)) {
return null;
}
List<PackageInfo> packInfos = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packInfos == null) {
return null;
}
for (PackageInfo info : packInfos) {
ProviderInfo[] providers = info.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (permission.equals(provider.readPermission) || permission.equals(provider.writePermission)) {
return provider.authority;
}
}
}
}
return null;
}
boolean isInstallShortcut = false;
final ContentResolver cr = context.getContentResolver();
String AUTHORITY = getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS");
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI, new String[] {"title"}, "title=?", new String[] {context.getString(R.string.app_name)}, null);
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
}
if (c != null) {
c.close();
}
return isInstallShortcut;
}
public static String getAuthorityFromPermission(Context context, String permission) {
if (TextUtils.isEmpty(permission)) {
return null;
}
List<PackageInfo> packInfos = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packInfos == null) {
return null;
}
for (PackageInfo info : packInfos) {
ProviderInfo[] providers = info.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (permission.equals(provider.readPermission) || permission.equals(provider.writePermission)) {
return provider.authority;
}
}
}
}
return null;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:Java占位符替换工具类
最新资讯
热门推荐