拨打电话时候控制权限,
加入v4依赖
在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" }
清单文件中加权限
- <uses-permission android:name="android.permission.CALL_PHONE"/>
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.menglucywhh.day1208_create.MainActivity">
- <Button
- android:id="@+id/btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- />
- </LinearLayout>
- import android.Manifest;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- startActivity(new Intent(this, PermissionsDispatcherMainActivity.class));
- findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // call();
- // checkSelfPermission 监测是否有 权限
- if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
- // PackageManager.PERMISSION_DENIED 拒绝
- // PackageManager.PERMISSION_GRANTED 已经授权
- // 请求授权 获取当前权限
- if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CALL_PHONE)) {
- new AlertDialog.Builder(MainActivity.this)
- .setMessage("app需要开启权限才能使用此功能")
- .setPositiveButton("设置", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- // 跳转到app设置
- ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
- }
- })
- .setNegativeButton("取消", null)
- .create()
- .show();
- } else {
- ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
- }
- } else {
- call();
- }
- }
- });
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- switch (requestCode) {
- case 1:
- //拨打电话
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // 表示用户授权
- call();
- } else {
- //用户拒绝了当前权限
- Toast.makeText(this, " 却少拨打电话的权限 ", Toast.LENGTH_SHORT).show();
- }
- break;
- default:
- break;
- }
- }
- private void call() {
- Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:18999999999"));
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
- // TODO: Consider calling
- // ActivityCompat#requestPermissions
- // here to request the missing permissions, and then overriding
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
- // int[] grantResults)
- // to handle the case where the user grants the permission. See the documentation
- // for ActivityCompat#requestPermissions for more details.
- return;
- }
- startActivity(intent);
- }
- }
PermissionsDispatcherMainActivity
- import android.Manifest;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.view.View;
- import android.widget.Toast;
- import permissions.dispatcher.NeedsPermission;
- import permissions.dispatcher.OnNeverAskAgain;
- import permissions.dispatcher.OnPermissionDenied;
- import permissions.dispatcher.OnShowRationale;
- import permissions.dispatcher.PermissionRequest;
- import permissions.dispatcher.RuntimePermissions;
- @RuntimePermissions
- public class PermissionsDispatcherMainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- PermissionsDispatcherMainActivityPermissionsDispatcher.callWithPermissionCheck(PermissionsDispatcherMainActivity.this);
- // PermissionsDispatcherMainActivityPermissionsDispatcher.toCallWithPermissionCheck(PermissionsDispatcherMainActivity.this);
- }
- });
- }
- // NeedsPermission 表示 改方法需要那些权限
- @NeedsPermission(Manifest.permission.CALL_PHONE)
- void call() {
- Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:18999999999"));
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
- // TODO: Consider calling
- // ActivityCompat#requestPermissions
- // here to request the missing permissions, and then overriding
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
- // int[] grantResults)
- // to handle the case where the user grants the permission. See the documentation
- // for ActivityCompat#requestPermissions for more details.
- return;
- }
- startActivity(intent);
- }
- // OnShowRationale 为什么需要当前权限 给用户一个解释
- @OnShowRationale(Manifest.permission.CALL_PHONE)
- void showRationaleForCamera(final PermissionRequest request) {
- new AlertDialog.Builder(this)
- .setMessage("title")
- .setPositiveButton("ok", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // proceed 获取当前权限
- request.proceed();
- }
- })
- .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // cancel 取消当前权限的申请
- request.cancel();
- }
- })
- .show();
- }
- // /OnPermissionDenied 权限被用户拒绝
- @OnPermissionDenied(Manifest.permission.CALL_PHONE)
- void showDeniedForCamera() {
- Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
- }
- // 表示用户勾选了不再提示
- @OnNeverAskAgain(Manifest.permission.CALL_PHONE)
- void showNeverAskForCamera() {
- Toast.makeText(this, "不再提示", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- // 把授权的结果 让 PermissionsDispatcherMainActivityPermissionsDispatcher 处理
- PermissionsDispatcherMainActivityPermissionsDispatcher.onRequestPermissionsResult(this,requestCode,grantResults);
- }
- }