Android判斷某個(gè)權(quán)限是否開啟的方法
更新時(shí)間:2018年07月31日 11:34:29 作者:Android______
今天小編就為大家分享一篇Android判斷某個(gè)權(quán)限是否開啟的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
/**
* 讀寫權(quán)限 自己可以添加需要判斷的權(quán)限
*/
public static String[]permissionsREAD={
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE };
/**
* 判斷權(quán)限集合
* permissions 權(quán)限數(shù)組
* return true-表示沒有改權(quán)限 false-表示權(quán)限已開啟
*/
public static boolean lacksPermissions(Context mContexts,permissionsREAD) {
for (String permission : permissions) {
if (lacksPermission(mContexts,permission)) {
return true;
}
}
return false;
}
/**
* 判斷是否缺少權(quán)限
*/
private static boolean lacksPermission(Context mContexts, String permission) {
return ContextCompat.checkSelfPermission(mContexts, permission) ==
PackageManager.PERMISSION_DENIED;
}
//Activity使用
if (lacksPermissions()){//讀寫權(quán)限沒開啟
ActivityCompat.requestPermissions(this,permissionsREAD,0);
}else {
//讀寫權(quán)限已開啟
}
//權(quán)限設(shè)置回調(diào)
@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,"權(quán)限設(shè)置成功");
}else {
//T.showShort(mContext,"拒絕權(quán)限");
// 權(quán)限被拒絕,彈出dialog 提示去開啟權(quán)限
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="需要手動(dòng)開啟權(quán)限才能使用"/>
<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="去設(shè)置"
android:layout_marginLeft="20dp"
android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>

以上這篇Android判斷某個(gè)權(quán)限是否開啟的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Android從相冊中獲取圖片出錯(cuò)圖片卻無法裁剪問題的方法
這篇文章主要介紹了解決Android從相冊中獲取圖片出錯(cuò)圖片卻無法裁剪問題的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
簡介Android應(yīng)用中sharedPreferences類存儲(chǔ)數(shù)據(jù)的用法
這篇文章主要介紹了Android應(yīng)用中使用sharedPreferences類存儲(chǔ)數(shù)據(jù)的方法,文中舉了用SharedPreferences保存數(shù)據(jù)和讀取數(shù)據(jù)的例子,需要的朋友可以參考下2016-02-02
Android開發(fā)實(shí)現(xiàn)ListView和adapter配合顯示圖片和文字列表功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ListView和adapter配合顯示圖片和文字列表功能,涉及Android使用ListView結(jié)合adapter適配器實(shí)現(xiàn)圖文顯示功能相關(guān)的布局、解析、權(quán)限控制等操作技巧,需要的朋友可以參考下2019-04-04
Android camera2 判斷相機(jī)功能是否可控的實(shí)例
下面小編就為大家?guī)硪黄狝ndroid camera2 判斷相機(jī)功能是否可控的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03

