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自定义View实现炫酷进度条

    Android自定义View实现炫酷进度条

    这篇文章主要为大家详细介绍了Android自定义View实现炫酷进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Android入门之使用RecyclerView完美实现瀑布流界面详解

    Android入门之使用RecyclerView完美实现瀑布流界面详解

    网上充满着不完善的基于RecyclerView的瀑布流实现,要么根本是错的、要么就是只知其一不知其二。本文就来用RecyclerView完美实现瀑布流界面,希望大家有所帮助
    2023-02-02
  • Android实现左侧滑动菜单

    Android实现左侧滑动菜单

    这篇文章主要为大家详细介绍了Android实现左侧滑动菜单,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Android中利用ViewHolder优化自定义Adapter的写法(必看)

    Android中利用ViewHolder优化自定义Adapter的写法(必看)

    下面小编就为大家带来一篇Android中利用ViewHolder优化自定义Adapter的写法(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Android中Glide加载图片并实现图片缓存

    Android中Glide加载图片并实现图片缓存

    本篇文章主要介绍了Android中Glide加载图片并实现图片缓存,这里和大家简单的分享一下Glide的使用方法以及缓存 ,有兴趣的可以了解一下。
    2017-03-03
  • android downsample降低音频采样频率代码

    android downsample降低音频采样频率代码

    这篇文章主要介绍了android downsample降低音频采样频率代码,需要的朋友可以参考下
    2014-02-02
  • flutter 屏幕尺寸适配和字体大小适配的实现

    flutter 屏幕尺寸适配和字体大小适配的实现

    这篇文章主要介绍了flutter 屏幕尺寸适配和字体大小适配的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • Android Studio打包.so库到apk中实例详解

    Android Studio打包.so库到apk中实例详解

    这篇文章主要介绍了Android Studio打包.so库到apk中实例详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • Flutter之 ListView组件使用示例详解

    Flutter之 ListView组件使用示例详解

    这篇文章主要为大家介绍了Flutter之 ListView组件使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Android编程之光线传感器用法详解

    Android编程之光线传感器用法详解

    这篇文章主要介绍了Android编程之光线传感器用法,结合实例形式分析了Android光线传感器的功能、实现步骤与相关注意事项,并给出了相关demo示例,需要的朋友可以参考下
    2017-11-11

最新评论