Android6.0动态权限

时间:2022-05-15 15:25:55

拨打电话时候控制权限,

加入v4依赖

Android6.0动态权限

在build.gradle里面加入如下依赖 ,

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
 
implementation 'com.android.support:support-v4:26.0.2'//我这里把27改成了26
// GitHub 上搜索 PermissionsDispatcher 点击第一个 Java 目录下的文章导入依赖 ( ${latest.version} 改为 3.0.1 compile('com.github.hotchemi:permissionsdispatcher:3.0.1') { // if you don't use android.app.Fragment you can exclude support for them exclude module: "support-v13" } annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:3.0.1" }

清单文件中加权限
[html] view plain copy
  1. <uses-permission android:name="android.permission.CALL_PHONE"/>  

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="com.example.menglucywhh.day1208_create.MainActivity">  
  8.   
  9.     <Button  
  10.         android:id="@+id/btn"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Hello World!"  
  14.         />  
  15.   
  16. </LinearLayout>  
MainActivity
  1. import android.Manifest;  
  2. import android.app.AlertDialog;  
  3. import android.content.DialogInterface;  
  4. import android.content.Intent;  
  5. import android.content.pm.PackageManager;  
  6. import android.net.Uri;  
  7. import android.support.annotation.NonNull;  
  8. import android.support.v4.app.ActivityCompat;  
  9. import android.support.v4.content.ContextCompat;  
  10. import android.support.v7.app.AppCompatActivity;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends AppCompatActivity {  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.   
  22.   
  23.         startActivity(new Intent(this, PermissionsDispatcherMainActivity.class));  
  24.   
  25.         findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {  
  26.             @Override  
  27.             public void onClick(View v) {  
  28.   
  29. //                call();  
  30.   
  31. //                checkSelfPermission 监测是否有 权限  
  32.                 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {  
  33. //                    PackageManager.PERMISSION_DENIED    拒绝  
  34. //                    PackageManager.PERMISSION_GRANTED    已经授权  
  35.   
  36.                     //  请求授权 获取当前权限  
  37.   
  38.                     if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CALL_PHONE)) {  
  39.                         new AlertDialog.Builder(MainActivity.this)  
  40.                                 .setMessage("app需要开启权限才能使用此功能")  
  41.                                 .setPositiveButton("设置", new DialogInterface.OnClickListener() {  
  42.                                     @Override  
  43.                                     public void onClick(DialogInterface dialogInterface, int i) {  
  44.                                         // 跳转到app设置  
  45.                                         ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);  
  46.                                     }  
  47.                                 })  
  48.                                 .setNegativeButton("取消", null)  
  49.                                 .create()  
  50.                                 .show();  
  51.                     } else {  
  52.                         ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);  
  53.                     }  
  54.   
  55.                 } else {  
  56.                     call();  
  57.                 }  
  58.             }  
  59.         });  
  60.   
  61.     }  
  62.     @Override  
  63.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {  
  64.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);  
  65.         switch (requestCode) {  
  66.             case 1:  
  67.                 //拨打电话  
  68.                 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
  69.   
  70.                     // 表示用户授权  
  71.                     call();  
  72.                 } else {  
  73.                     //用户拒绝了当前权限  
  74.   
  75.                     Toast.makeText(this, " 却少拨打电话的权限 ", Toast.LENGTH_SHORT).show();  
  76.                  }  
  77.   
  78.                 break;  
  79.   
  80.             default:  
  81.                 break;  
  82.   
  83.         }  
  84.   
  85.     }  
  86.   
  87.     private void call() {  
  88.         Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:18999999999"));  
  89.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  90.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {  
  91.             // TODO: Consider calling  
  92.             //    ActivityCompat#requestPermissions  
  93.             // here to request the missing permissions, and then overriding  
  94.             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,  
  95.             //                                          int[] grantResults)  
  96.             // to handle the case where the user grants the permission. See the documentation  
  97.             // for ActivityCompat#requestPermissions for more details.  
  98.             return;  
  99.         }  
  100.         startActivity(intent);  
  101.     }  
  102. }  
PermissionsDispatcherMainActivity
  1. import android.Manifest;  
  2. import android.app.Activity;  
  3. import android.app.AlertDialog;  
  4. import android.content.DialogInterface;  
  5. import android.content.Intent;  
  6. import android.content.pm.PackageManager;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.support.annotation.NonNull;  
  10. import android.support.v4.app.ActivityCompat;  
  11. import android.support.v4.content.ContextCompat;  
  12. import android.view.View;  
  13. import android.widget.Toast;  
  14.   
  15. import permissions.dispatcher.NeedsPermission;  
  16. import permissions.dispatcher.OnNeverAskAgain;  
  17. import permissions.dispatcher.OnPermissionDenied;  
  18. import permissions.dispatcher.OnShowRationale;  
  19. import permissions.dispatcher.PermissionRequest;  
  20. import permissions.dispatcher.RuntimePermissions;  
  21.   
  22. @RuntimePermissions  
  23. public class PermissionsDispatcherMainActivity extends Activity {  
  24.   
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.   
  31.         findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {  
  32.             @Override  
  33.             public void onClick(View v) {  
  34.   
  35.                 PermissionsDispatcherMainActivityPermissionsDispatcher.callWithPermissionCheck(PermissionsDispatcherMainActivity.this);  
  36. //                PermissionsDispatcherMainActivityPermissionsDispatcher.toCallWithPermissionCheck(PermissionsDispatcherMainActivity.this);  
  37.   
  38.             }  
  39.         });  
  40.     }  
  41.   
  42.     //    NeedsPermission 表示 改方法需要那些权限  
  43.     @NeedsPermission(Manifest.permission.CALL_PHONE)  
  44.     void call() {  
  45.         Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:18999999999"));  
  46.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  47.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {  
  48.             // TODO: Consider calling  
  49.             //    ActivityCompat#requestPermissions  
  50.             // here to request the missing permissions, and then overriding  
  51.             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,  
  52.             //                                          int[] grantResults)  
  53.             // to handle the case where the user grants the permission. See the documentation  
  54.             // for ActivityCompat#requestPermissions for more details.  
  55.             return;  
  56.         }  
  57.         startActivity(intent);  
  58.     }  
  59.   
  60.   
  61. //    OnShowRationale 为什么需要当前权限  给用户一个解释  
  62.     @OnShowRationale(Manifest.permission.CALL_PHONE)  
  63.     void showRationaleForCamera(final PermissionRequest request) {  
  64.         new AlertDialog.Builder(this)  
  65.                 .setMessage("title")  
  66.                 .setPositiveButton("ok", new DialogInterface.OnClickListener() {  
  67.                     @Override  
  68.                     public void onClick(DialogInterface dialog, int which) {  
  69. //                        proceed 获取当前权限  
  70.                         request.proceed();  
  71.                     }  
  72.                 })  
  73.                 .setNegativeButton("cancel", new DialogInterface.OnClickListener() {  
  74.                     @Override  
  75.                     public void onClick(DialogInterface dialog, int which) {  
  76. //                        cancel 取消当前权限的申请  
  77.                         request.cancel();  
  78.                     }  
  79.                 })  
  80.                 .show();  
  81.     }  
  82.   
  83. //    /OnPermissionDenied  权限被用户拒绝  
  84.     @OnPermissionDenied(Manifest.permission.CALL_PHONE)  
  85.     void showDeniedForCamera() {  
  86.         Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();  
  87.     }  
  88.   
  89. //    表示用户勾选了不再提示  
  90.     @OnNeverAskAgain(Manifest.permission.CALL_PHONE)  
  91.     void showNeverAskForCamera() {  
  92.         Toast.makeText(this, "不再提示", Toast.LENGTH_SHORT).show();  
  93.     }  
  94.   
  95.   
  96.     @Override  
  97.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {  
  98.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);  
  99.   
  100.         // 把授权的结果 让 PermissionsDispatcherMainActivityPermissionsDispatcher 处理  
  101.         PermissionsDispatcherMainActivityPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults);  
  102.     }