Android判断某个权限是否开启的方法

 更新时间:2018年07月31日 11:34:29   作者:Android______  
今天小编就为大家分享一篇Android判断某个权限是否开启的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

/**
 * 读写权限 自己可以添加需要判断的权限
 */
public static String[]permissionsREAD={
    Manifest.permission.READ_EXTERNAL_STORAGE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE };
/**
 * 判断权限集合
 * permissions 权限数组
 * return true-表示没有改权限 false-表示权限已开启
 */
public static boolean lacksPermissions(Context mContexts,permissionsREAD) {
  for (String permission : permissions) {
    if (lacksPermission(mContexts,permission)) {
      return true;
    }
  }
  return false;
}

/**
 * 判断是否缺少权限
 */
private static boolean lacksPermission(Context mContexts, String permission) {
  return ContextCompat.checkSelfPermission(mContexts, permission) ==
      PackageManager.PERMISSION_DENIED;
}

//Activity使用

if (lacksPermissions()){//读写权限没开启
  ActivityCompat.requestPermissions(this,permissionsREAD,0);
}else {
  //读写权限已开启
}

//权限设置回调

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  if (requestCode==0){
    for (int i = 0; i < permissions.length; i++) {
      if (grantResults[i]!=-1){
        //T.showShort(mContext,"权限设置成功");
        
      }else {
        //T.showShort(mContext,"拒绝权限");
        // 权限被拒绝,弹出dialog 提示去开启权限
        showPermissions();
        break;
      }
    }

  }
}
//弹出dialog
private void showPermissions(){
  final Dialog dialog=new android.app.AlertDialog.Builder(mContext).create();
  View v=LayoutInflater.from(mContext).inflate(R.layout.dialog_permissions,null);
  dialog.show();
  dialog.setContentView(v);

  Button btn_add= (Button) v.findViewById(R.id.btn_add);
  Button btn_diss= (Button) v.findViewById(R.id.btn_diss);

  btn_add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      dialog.dismiss();
      Intent intent = new Intent();
      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
      intent.addCategory(Intent.CATEGORY_DEFAULT);
      intent.setData(Uri.parse("package:" + getPackageName()));
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
      intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
      startActivity(intent);
    }
  });

  btn_diss.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      dialog.dismiss();
    }
  });
}

//dialog布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="250dp"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:background="@color/white"
  android:orientation="vertical" >

  <TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    android:text="提示"
    android:background="@color/zhuti_color"
    android:textColor="@color/white"
    android:textSize="16sp" />


  <TextView
    android:id="@+id/tv_hint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/zhuti_color"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:textSize="16sp"
    android:gravity="center"
    android:lineSpacingExtra="3dp"
    android:text="需要手动开启权限才能使用"/>

  <TextView
    android:id="@+id/tv_hint2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/zhuti_color"
    android:layout_margin="10dp"
    android:textSize="12sp"
    android:gravity="center"
    android:visibility="gone"
    android:lineSpacingExtra="3dp"
    android:text=""/>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:gravity="center"
    android:orientation="horizontal">
    <Button
      android:id="@+id/btn_diss"
      android:layout_width="0dp"
      android:layout_weight="1"
      android:layout_height="30dp"
      android:background="@color/line2"
      android:text="取消"
      />
    <Button
      android:id="@+id/btn_add"
      android:layout_width="0dp"
      android:layout_weight="1"
      android:layout_height="30dp"
      android:layout_gravity="center"
      android:background="@color/zhuti_color"
      android:text="去设置"
      android:layout_marginLeft="20dp"
      android:textColor="@color/white" />

  </LinearLayout>


</LinearLayout>

以上这篇Android判断某个权限是否开启的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Android 侧滑按钮的实现代码

    Android 侧滑按钮的实现代码

    这篇文章主要介绍了Android 侧滑按钮的实现,本文结合示例代码图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • Android编程之json解析实例详解

    Android编程之json解析实例详解

    这篇文章主要介绍了Android编程之json解析,结合实例形式较为详细的分析了了Android针对json格式数据的创建、读取与解析技巧,需要的朋友可以参考下
    2015-12-12
  • Android评论功能的实现过程

    Android评论功能的实现过程

    这篇文章为大家详细介绍了Android评论功能的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Jetpack Compose对比React Hooks API相似度

    Jetpack Compose对比React Hooks API相似度

    这篇文章主要为大家介绍了Jetpack Compose对比React Hooks API相似度,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Android中button的onClick事件几种方法

    Android中button的onClick事件几种方法

    这篇文章主要介绍了Android中button的onClick事件几种方法的相关资料,这里提供三种方法,实现监听事件,需要的朋友可以参考下
    2017-09-09
  • Android自定义控件之继承ViewGroup创建新容器

    Android自定义控件之继承ViewGroup创建新容器

    这篇文章主要介绍了Android自定义控件之继承ViewGroup创建新容器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • 详解Andorid开发中反射机制是怎么一回事

    详解Andorid开发中反射机制是怎么一回事

    反射机制是在运行状态中,对于任何一个类,都可以知道这个类的所有属性和方法,对于任何一个对象,都可以调用它所有的方法和属性,修改部分类型信息,这种动态获取信息以及动态调用对象方法的功能称为Java的反射机制
    2022-11-11
  • Android客户端与服务端数据加密传输方案详解

    Android客户端与服务端数据加密传输方案详解

    这篇文章主要为大家介绍了Android客户端与服务端数据加密传输方案详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android ExpandableListView使用方法案例详解

    Android ExpandableListView使用方法案例详解

    这篇文章主要介绍了Android ExpandableListView使用方法案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Android开发 Activity和Fragment详解

    Android开发 Activity和Fragment详解

    本文主要介绍Android开发 Activity和Fragment,这里对Activity和Fragment的知识做了详细讲解,并附简单代码示例,有兴趣的小伙伴可以参考下
    2016-08-08

最新评论