自学内容网 自学内容网

Android——运行时动态申请权限

  • 检查App是否开启了指定权限

调用ContextCompat.checkSelfPermission

  • 请求系统弹出弹窗,以便用户选择是否开启权限

调用ActivityCompat.requestPermissions方法,打开系统权限申请弹窗

  • 判断用户权限选择结果

重写活动页面的权限请求回调方法onRequestPermissionsResult,在该方法内部处理用户的权限选择结果

package com.example.cpclient;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.cpclient.util.PermissionUtil;


public class PermissionLazyActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String[] PERMISSION_CONTACTS = new String[]{
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS,
    };

    private static final String[] PERMISSION_SMS = new String[]{
            Manifest.permission.READ_SMS,
            Manifest.permission.SEND_SMS,
    };

    private static final int REQUEST_CONTACTS_CODE = 1;
    private static final int REQUEST_SMS_CODE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_lazy);

        findViewById(R.id.contacts).setOnClickListener(this);
        findViewById(R.id.sms).setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.contacts:
                PermissionUtil.checkPermission(this, PERMISSION_CONTACTS, REQUEST_CONTACTS_CODE);
                break;
            case R.id.sms:
                PermissionUtil.checkPermission(this, PERMISSION_SMS, REQUEST_SMS_CODE);
                break;
        }
    }


    // 给用户打开请求权限系统弹窗后,用户操作的回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.d("AAAA",Integer.toString(requestCode));
        switch (requestCode) {
            case REQUEST_CONTACTS_CODE:
                if (PermissionUtil.checkGrant(grantResults)) {
                    Log.d("AAAA", "通讯录权限获取成功");
                } else {
                    jumpToSettings();
                }
                break;
            case REQUEST_SMS_CODE:
                if (PermissionUtil.checkGrant(grantResults)) {
                    Log.d("AAAA", "短信权限获取成功");
                } else {
                    jumpToSettings();
                }
                break;
        }
    }

    private void jumpToSettings() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", getPackageName(), null));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
package com.example.cpclient.util;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class PermissionUtil {
    // 多个权限返回true表示有权限,false没有开启权限
    public static boolean checkPermission(Activity act, String[] permissions, int requestCode) {
        // Android6.0之后开始采用动态权限管理
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checkCode = PackageManager.PERMISSION_GRANTED;
            for (String permission : permissions) {
                checkCode = ContextCompat.checkSelfPermission(act, permission);
                if (checkCode != PackageManager.PERMISSION_GRANTED) {
                    break;
                }
            }

            // 未开启权限,请求系统弹窗,好让用户选择是否开启权限
            if (checkCode != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(act, permissions, requestCode);
                return false;
            }
        }

        return true;
    }

    public static boolean checkGrant(int[] grantResults) {
        if (grantResults != null) {
            for (int grant : grantResults) {
                if (grant != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}

案例代码


原文地址:https://blog.csdn.net/weixin_45932565/article/details/142579637

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!