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

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

本人使用小米手机,打开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

标签:Android,6.0,权限,设置
0
投稿

猜你喜欢

  • 详解如何使用Android Studio开发Gradle插件

    2023-11-20 23:39:05
  • Android 性能优化系列之bitmap图片优化

    2023-11-05 14:03:20
  • Deepin系统安装eclipse2021-03及CDT插件的安装教程

    2022-02-12 05:42:33
  • android自定义imageview实现圆角图片

    2023-05-12 07:12:07
  • java线程池:获取运行线程数并控制线程启动速度的方法

    2022-06-30 23:22:57
  • Java使用JDBC连接postgresql数据库示例

    2022-11-06 22:49:02
  • Android编程获取GPS数据的方法详解

    2023-09-20 16:37:34
  • 浅谈Java工程读取resources中资源文件路径的问题

    2021-07-20 19:13:45
  • SpringBoot如何实现word文档转pdf

    2023-04-19 09:33:55
  • 解决Spring Security中AuthenticationEntryPoint不生效相关问题

    2022-11-29 06:53:09
  • Spring boot中@Conditional和spring boot的自动配置实例详解

    2023-06-20 09:36:14
  • Java多线程 两阶段终止模式Two-Phase Termination Patter

    2023-11-29 04:47:04
  • Java 中的Printstream介绍_动力节点Java学院整理

    2021-12-21 06:08:36
  • java创建多级目录文件的实例讲解

    2023-11-27 09:29:02
  • android打开本地图像的方法

    2022-10-26 08:01:42
  • SpringBoot结合JSR303对前端数据进行校验的示例代码

    2022-09-15 03:22:55
  • JAVA8 十大新特性详解

    2023-07-02 10:03:27
  • springmvc拦截器登录验证示例

    2022-11-28 12:15:24
  • SpringBoot整合freemarker实现代码生成器

    2023-07-17 20:31:08
  • Redis集群与SSM整合使用方法

    2023-07-02 02:17:05
  • asp之家 软件编程 m.aspxhome.com