android 6.0權限全面詳細分析和解決方案

 

1、Android6.0規定的危險權限危險權限android

 

Permission Group Permissions
android.permission-group.CALENDAR
  • android.permission.READ_CALENDAR
  • android.permission.WRITE_CALENDAR
android.permission-group.CAMERA
  • android.permission.CAMERA
android.permission-group.CONTACTS
  • android.permission.READ_CONTACTS
  • android.permission.WRITE_CONTACTS
  • android.permission.GET_ACCOUNTS
android.permission-group.LOCATION
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.ACCESS_COARSE_LOCATION
android.permission-group.MICROPHONE
  • android.permission.RECORD_AUDIO
android.permission-group.PHONE
  • android.permission.READ_PHONE_STATE
  • android.permission.CALL_PHONE
  • android.permission.READ_CALL_LOG
  • android.permission.WRITE_CALL_LOG
  • com.android.voicemail.permission.ADD_VOICEMAIL
  • android.permission.USE_SIP
  • android.permission.PROCESS_OUTGOING_CALLS
android.permission-group.SENSORS
  • android.permission.BODY_SENSORS
android.permission-group.SMS
  • android.permission.SEND_SMS
  • android.permission.RECEIVE_SMS
  • android.permission.READ_SMS
  • android.permission.RECEIVE_WAP_PUSH
  • android.permission.RECEIVE_MMS
  • android.permission.READ_CELL_BROADCASTS
android.permission-group.STORAGE
  • android.permission.READ_EXTERNAL_STORAGE
  • android.permission.WRITE_EXTERNAL_STORAGE

 

2、假如在App中須要進行撥打電話的操做,當點擊按鈕,直接撥出一個電話ide

 

代碼以下:ui

 this

1 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:10086"));
2 startActivity(intent);

可是若是你的編譯版本是23或者以上,那麼Android Studio就會報出一個錯誤spa

 

 若是咱們直接對這個錯誤進行自動修復,會在startActivity方法以前增長一段代碼用於權限檢查,若是當前沒有賦予該權限,則直接return結束方法:code

 1 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
 2       // TODO: Consider calling
 3       //    ActivityCompat#requestPermissions
 4       // here to request the missing permissions, and then overriding
 5       //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
 6       //                                          int[] grantResults)
 7       // to handle the case where the user grants the permission. See the documentation
 8       // for ActivityCompat#requestPermissions for more details.
 9       return;
10 }

由於i是直接return 打電話功能沒有實現blog

ci

咱們能夠在點擊按鈕的時候,直接向用戶請求權限,能夠使用下面的方法,在上段代碼return前加上次句:get

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUESTCODE);

注:第三個參數爲請求碼,整型it

 

效果以下

 

容許:之後再也不彈出請求框,直接容許應用得到該權限

拒絕:下次須要時會從新彈出,直到用戶選擇容許,不然會不斷出現

勾選再也不詢問+拒絕之後不會再彈出,而且默認拒絕權限請求,也就是說,應用不再能得到這個權限,除非用戶主動進入應用設置中從新受權。

 

 

爲了提升用戶體驗,咱們能夠本身在出現上述的第三種狀況時再次點擊窗口時咱們跳轉到應用設置界面

 

當咱們調用requestPermissions方法的時候,不管成功與否,都會將結果回調給Activity中的onRequestPermissionsResult方法中,咱們能夠經過重寫這個方法來處理

代碼以下:

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)) {
                    AskForPermission();
                }
            }
        }
    }

private void AskForPermission() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("須要賦予通話的權限,不開啓將沒法正常工做!");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("設置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName())); // 根據包名打開對應的設置界面
                startActivity(intent);
            }
        });
        builder.create().show();
    }

咱們能夠在用戶勾選和點擊拒絕時彈出提示,來解釋權限的必要性,並轉到應用設置界面:

代碼以下:

 1 @Override
 2     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
 3                                            @NonNull int[] grantResults) {
 4         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 5         /*if (requestCode == 1) {
 6             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
 7                 if (!shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)) {
 8                     AskForPermission();
 9                 }
10             }
11         }*/
12         if (requestCode == 1) {
13             if (permissions[0].equals(Manifest.permission.CALL_PHONE)
14                     && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
15             } else {
16                 //用戶不一樣意,向用戶展現該權限做用
17                 if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
18                     AlertDialog dialog = new AlertDialog.Builder(this)
19                             .setMessage("須要賦予通話的權限,不開啓將沒法正常工做!")
20                             .setPositiveButton("設置", new DialogInterface.OnClickListener() {
21                                 @Override
22                                 public void onClick(DialogInterface dialog, int which) {
23                                     Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
24                                     intent.setData(Uri.parse("package:" + getPackageName())); // 根據包名打開對應的設置界面
25                                     startActivity(intent);
26                                 }
27                             })
28                             .setNegativeButton("取消", new DialogInterface.OnClickListener() {
29                                 @Override
30                                 public void onClick(DialogInterface dialog, int which) {
31 
32                                 }
33                             }).create();
34                     dialog.show();
35                     return;
36                 }
37             }
38         }
39     }
40 
41     private void AskForPermission() {
42         AlertDialog.Builder builder = new AlertDialog.Builder(this);
43         builder.setTitle("須要權限");
44         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
45             @Override
46             public void onClick(DialogInterface dialog, int which) {
47 
48             }
49         });
50         builder.setPositiveButton("權限設置", new DialogInterface.OnClickListener() {
51             @Override
52             public void onClick(DialogInterface dialog, int which) {
53                 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
54                 intent.setData(Uri.parse("package:" + getPackageName())); // 根據包名打開對應的設置界面
55                 startActivity(intent);
56             }
57         });
58         builder.create().show();
59     }

注:我用的請求碼爲 1

相關文章
相關標籤/搜索