安卓动态申请权限

package top.foxhome.demo.myapplication;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {
    /**
     * 配置需要申请的权限
     * 注意即使是动态申请也需要再AndroidManifest中进行配置
     */
    private final String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    //当前的权限赋予状态
    private AllPermissionsStatus allPermissionsGranted = AllPermissionsStatus.Denied;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!checkPermissions())
            requestPermissions();
        else
            allPermissionsGranted = AllPermissionsStatus.Cranted;
    }

    /**
     * 检查权限
     *
     * @return
     */
    private boolean checkPermissions() {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED)
                return false;
        }
        return true;
    }

    /**
     * 权限状态枚举
     */
    enum AllPermissionsStatus {
        Cranted,
        DontAskAgain,
        Denied
    }

    private final int REQUEST_PERMISSIONS_CODE = 0x0981;
    private static final int NOT_NOTICE = 0x0982;//如果勾选了不再询问

    private void requestPermissions() {
        ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSIONS_CODE);
    }

    /**
     * 权限申请返回
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (REQUEST_PERMISSIONS_CODE == requestCode) {
            allPermissionsGranted = AllPermissionsStatus.Cranted;
            for (int index = 0; index < permissions.length; index++) {
                if (grantResults[index] == PackageManager.PERMISSION_GRANTED) {
                    Log.e("swlog", "选择了始终允许");
                } else if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[index])) {
                    Log.e("swlog", "用户选择了禁止不再询问");
                    allPermissionsGranted = AllPermissionsStatus.DontAskAgain;
                    break;
                } else {
                    allPermissionsGranted = AllPermissionsStatus.Denied;
                    Log.e("swlog", "选择了禁止");
                }
            }
            if (allPermissionsGranted == AllPermissionsStatus.DontAskAgain) {
                //跳转到权限授予界面
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);//注意就是"package",不用改成自己的包名
                intent.setData(uri);
                startActivityForResult(intent, NOT_NOTICE);
            } else if (allPermissionsGranted == AllPermissionsStatus.Denied) {
                requestPermissions();
            }
        }
    }

    /**
     * 跳转到权限界面后的回调
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (NOT_NOTICE == requestCode) {//从授权界面返回后再次进行权限判断
            if (checkPermissions()) {//授予所有权限
                allPermissionsGranted = AllPermissionsStatus.Cranted;
            } else {//仍旧存在未授予的权限
                allPermissionsGranted = AllPermissionsStatus.Denied;
                requestPermissions();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}

发表回复

CAPTCHAis initialing...