拨打电话解决6.0以上版本权限问题以及实现拒绝权限后再次申请权限

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;
import com.zk.cloudbill.gloabl.Configures;

import java.util.List;

/**
 * Created by yj on 2018/1/10/010.
 * 拨打电话的工具类
 */

public class CallUtils {
    public static void checkPermiss(final Activity context) {
        AndPermission.with(context)
                .requestCode(188)
                .permission(Manifest.permission.CALL_PHONE)
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List grantPermissions) {
                        if (AndPermission.hasPermission(context, grantPermissions)) {
                            // TODO 执行拥有权限时的下一步。
                            Uri uri = Uri.parse("tel:" + Configures.COMMPANY_PHONE);
                            Intent intent = new Intent(Intent.ACTION_CALL, uri);
                            if (ActivityCompat.checkSelfPermission(context, 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;
                            }
                            context.startActivity(intent);
                        } else {
                            // 使用AndPermission提供的默认设置dialog,用户点击确定后会打开App的设置页面让用户授权。
                            AndPermission.defaultSettingDialog(context, requestCode).show();

                        }
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List deniedPermissions) {
                        if (AndPermission.hasPermission(context, deniedPermissions)) {
                            // TODO 执行拥有权限时的下一步。
                            Uri uri = Uri.parse("tel:" + Configures.COMMPANY_PHONE);
                            Intent intent = new Intent(Intent.ACTION_CALL, uri);
                            if (ActivityCompat.checkSelfPermission(context, 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;
                            }
                            context.startActivity(intent);
                        } else {
                            // 使用AndPermission提供的默认设置dialog,用户点击确定后会打开App的设置页面让用户授权。
                            AndPermission.defaultSettingDialog(context, requestCode).show();

                        }
                    }
                })
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
                        // 这里的对话框可以自定义,只要调用rationale.resume()就可以继续申请。
                        new AlertDialog.Builder(context)
                                .setMessage("我们需要拨打电话限权限,请您手动授权,否则功能无法正常使用!")
                                .setPositiveButton("同意继续申请", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        rationale.resume();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        ToastUtil.showToast("您拒绝了拨打电话权限,当无法正常使用的时候,请去手动授权!");
                                    }
                                })
                                .show()
                                .setCanceledOnTouchOutside(false);
                    }
                })
                .start();
    }
}

你可能感兴趣的:(安卓开发)