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判断某个权限是否开启的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)