软件编程
位置:首页>> 软件编程>> Android编程>> Android 6.0以上权限拒绝打开权限设置界面的解决方法

Android 6.0以上权限拒绝打开权限设置界面的解决方法

作者:无暇之泪  发布时间:2021-11-07 01:59:35 

标签:Android,6.0,权限,设置

本人使用小米手机,打开qq或者微信的时候,某个权限拒绝的话,会提示你开启,点击开启会跳转到app的权限设置界面,当然了,这是国内系统深层定制的原因,也就是说这个界面原声的android没有的!这里以小米和魅族作为示例讲解如何让用户手动打开权限,当然了如果是原声的android就让他跳转到应用的详情设置页面(有点坑,因为普通用户还是不知道怎么整)。

参考了很多零零碎碎的东西,网址已经找不到了。。。。。。

ok,第一步是跳转到系统的界面,下面基本上可以从9开始考虑了,可以简化。


String SCHEME = "package";
 //调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.1及之前版本)
 final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
 //调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.2)
 final String APP_PKG_NAME_22 = "pkg";
 //InstalledAppDetails所在包名
 final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
 //InstalledAppDetails类名
 final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";

Intent intent = new Intent();
 final int apiLevel = Build.VERSION.SDK_INT;
 if (apiLevel >= 9) { // 2.3(ApiLevel 9)以上,使用SDK提供的接口
  intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  Uri uri = Uri.fromParts(SCHEME, getPackageName(), null);
  intent.setData(uri);
 } else { // 2.3以下,使用非公开的接口(查看InstalledAppDetails源码)
  // 2.2和2.1中,InstalledAppDetails使用的APP_PKG_NAME不同。
  final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
    : APP_PKG_NAME_21);
  intent.setAction(Intent.ACTION_VIEW);
  intent.setClassName(APP_DETAILS_PACKAGE_NAME,
    APP_DETAILS_CLASS_NAME);
  intent.putExtra(appPkgName, getPackageName());
 }
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

第二个,miui,首先你得判断是miui,亲自测试,MIUI7稳定版,MIUI8开发板本可行,工具类下面会提供下载


if (CheckPhoneSystemUtils.isMIUI()) {
  MLog.i("产品/硬件的制造商小米:");
  intent.setAction("miui.intent.action.APP_PERM_EDITOR");
  intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
  intent.putExtra("extra_pkgname", getPackageName());
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  try {
   startActivity(intent);
  } catch (Exception e) {
   e.printStackTrace();
   Toast.makeText(MediaRecoderService.this, "只有MIUI才可以设置哦", Toast.LENGTH_SHORT).show();
  }
 }

第三个,flyme(由于没有flyme机子),采用的云手机测试的


else if (CheckPhoneSystemUtils.isFlyme()) {
  intent.setAction("com.meizu.safe.security.SHOW_APPSEC");
  intent.addCategory(Intent.CATEGORY_DEFAULT);
  intent.putExtra("packageName", getPackageName());
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  try {
   startActivity(intent);
  } catch (Exception e) {
   e.printStackTrace();
   Toast.makeText(MediaRecoderService.this, "只有Flyme才可以设置哦", Toast.LENGTH_SHORT).show();
  }
 }

下面是工具类:BuildProperties


public class BuildProperties {
private final Properties properties;

private BuildProperties() throws IOException {
 properties = new Properties();
 properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
}

public boolean containsKey(final Object key) {
 return properties.containsKey(key);
}

public boolean containsValue(final Object value) {
 return properties.containsValue(value);
}

public Set<Map.Entry<Object, Object>> entrySet() {
 return properties.entrySet();
}

public String getProperty(final String name) {
 return properties.getProperty(name);
}

public String getProperty(final String name, final String defaultValue) {
 return properties.getProperty(name, defaultValue);
}

public boolean isEmpty() {
 return properties.isEmpty();
}

public Enumeration<Object> keys() {
 return properties.keys();
}

public Set<Object> keySet() {
 return properties.keySet();
}

public int size() {
 return properties.size();
}

public Collection<Object> values() {
 return properties.values();
}

public static BuildProperties newInstance() throws IOException {
 return new BuildProperties();
}

CheckPhoneSystemUtils


private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

/**
 * 检测MIUI
 *
 * @return
 */
public static boolean isMIUI() {
 try {
  final BuildProperties prop = BuildProperties.newInstance();
  return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
 } catch (final IOException e) {
  return false;
 }
}

/**
 * 检测Flyme
 *
 * @return
 */
public static boolean isFlyme() {
 try { // Invoke Build.hasSmartBar()
  final Method method = Build.class.getMethod("hasSmartBar");
  return method != null;
 } catch (final Exception e) {
  return false;
 }
}

来源:https://blog.csdn.net/qq_16382309/article/details/51724954

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com