Android动态权限

请看这里

tip1

判定是否有权限:checkSelfPermission()

tip2

如果没有权限,弹出dialog给用户选择:requestPermission(),第二个参数code与onRequestPermissionResult()方法中的code对应

if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {  
    requestPermissions(  
            new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },  
            REQUEST_CODE_ASK_PERMISSON);  
} 

tip3

判断用户是否确认了权限onRequestPermissionResult ()

public void onRequestPermissionsResult(int requestCode,  
        String permissions[], int[] grantResults) {  
    switch (requestCode) {  
    case REQUEST_CODE_ASK_PERMISSON:  
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
            // Permission Granted  
        } else {  
            // Permission Denied  
        }  
        break;  
    default:  
        super.onRequestPermissionsResult(requestCode, permissions,  
                grantResults);  

tip4

在弹出权限选择的对话框前给用户show一个dialog,用于引导用户进行选择。

if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {  
    if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {  
        // Explain to the user why we need to read the contacts  
        showMessage("请允许应用对SD卡进行读写操作",  
                new DialogInterface.OnClickListener() {  
                    @Override  
                    public void onClick(DialogInterface dialog,  
                            int which) {  
                        requestPermissions(  
                                new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },  
                                REQUEST_CODE_ASK_PERMISSIONS);  
                    }  
                });  
        return;  
    }  
    requestPermissions(  
            new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },  
            REQUEST_CODE_ASK_PERMISSIONS);  
}  

private void showMessage(String message,  
        DialogInterface.OnClickListener okListener) {  
    new AlertDialog.Builder(MainActivity.this).setMessage(message)  
            .setPositiveButton("OK", okListener).create().show();  
} 

多权限循环授权

private String[] permissions = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,};

private List<String> permissionList = new ArrayList<>();


private void addPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(permissions[0]);
            }

            if (checkSelfPermission(permissions[1]) != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(permissions[1]);
            }

            if (!permissionList.isEmpty()) {
                String[] permission = permissionList.toArray(new String[permissionList.size()]);
                requestPermissions(permission, 1);
            }
        }
    }

Service等无界面应用动态权限授予

public class PermissionActivity extends Activity {
    private static final String TAG = "PermissionActivity";
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_permission);
        mContext = this;
        requestPermissions(
                new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                1);
        Log.i(TAG, "request permisson");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    Log.i(TAG, "get permisson");
                    Log.i(TAG, "finish PermissionActivity");
                    Constants.PERMISSION = false;
                    finish();
                } else {
                    // Permission Denied
                    Log.i(TAG, "refuse permisson");
                    Log.i(TAG, "finish PermissionActivity");
                    finish();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions,
                        grantResults);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值