Android权限申请-多个权限

       注意:调用权限的位置一定要放在使用权限的功能之前。比如首页需要定位权限,你需要在启动页或者开始定位之前的位置,进行权限配置。

基于PermissionsDispatcher   

package com.ycz.at_cdm_station;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.transformers.common.mvp.BaseActivity;
import com.transformers.common.provider.IMainProvider;
import com.transformers.common.util.SystemUtil;
import com.transformers.common.util.ToastUtil;
import com.transformers.common.util.log.KLogUtil;
import com.transformers.main.MainActivity;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

/**
 * author: jiaxufei(<a href="mailto:jiaxufei@danlu.com">jiaxufei@danlu.com</a>)<br>
 * version: 1.0.0<br>
 * since: 2020-03-27 16:54<br>
 *
 * <p>
 * 内容描述区域
 * </p>
 */
@RuntimePermissions
public class SplashActivity extends BaseActivity {
    //是否已经授权
    private boolean isAllowPermissions = false;

    @Override
    public int onBindLayout() {
        return R.layout.activity_main_splash;
    }

    @Override
    public void initView() {
        if (!isAllowPermissions) {
            SplashActivityPermissionsDispatcher.requestPermissionWithPermissionCheck(SplashActivity.this);
        } else {
            requestPermission();
        }

        //全屏
        Window window = getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        startMainActivity();

    }

    @Override
    public boolean enableToolbar() {
        return false;
    }

    @Override
    public void initData() {

    }

    public void startMainActivity() {
//        startActivity(new Intent(this, MainActivity.class));
//        finish();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
       SplashActivityPermissionsDispatcher.onRequestPermissionsResult(  SplashActivity.this, requestCode, grantResults);
    }
    /**
     * 只有全部成功时,才会调用  2,拨打电话需要CALL_PHONE权限,在对应的方法是标明
     */
    @NeedsPermission({Manifest.permission.CALL_PHONE,Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE})
    public void requestPermission() {
        isAllowPermissions = true;
        Toast.makeText(SplashActivity.this, "申请权限成功", Toast.LENGTH_SHORT).show();

    }
    /**
     * 只要有一个权限没有申请成功,且没有选择"以后不再询问",则提示失败
     */
    @OnPermissionDenied({Manifest.permission.CALL_PHONE,Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE})
    public void requestPermissionDenied() {
        Toast.makeText(SplashActivity.this, "申请权限失败", Toast.LENGTH_SHORT).show();

    }
    /**
     * 申请失败后,再申请则调用 对需要该权限的解释
     *
     * @param permissionRequest
     */
    @OnShowRationale({Manifest.permission.CALL_PHONE,Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE})
    public void requestPermissionShowRationale(final PermissionRequest permissionRequest) {
        new AlertDialog.Builder(this).setMessage("是否授权手机权限")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//确定后,则弹出申请权限的提示
                        permissionRequest.proceed();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//取消后,调用requestPermissionDenied
                        permissionRequest.cancel();
                    }
                }).show();
    }
    /**
     * 只要有一个申请失败,且选择了"以后不再询问",则提示
     */
    @OnNeverAskAgain({Manifest.permission.CALL_PHONE,Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,})
    public void requestPermissionNeverAskAgain() {
        new AlertDialog.Builder(this)
                .setMessage("应用权限被拒绝,为了不影响您的正常使用,请在 权限 中开启对应权限?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //引导用户至设置页手动授权
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", SplashActivity.this.getPackageName(), null);
                        intent.setData(uri);
                        startActivity(intent);
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .show();    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄毛火烧雪下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值