android studio 6.0以上动态申请权限的代码

下午用小米真机调试时发现权限明明已经在AndroidManifest.xml中添加过了,但还是没有申请到应该有的权限,解决办法是下面的代码

	//读写权限 具体权限加在字符串里面
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    
    //请求状态码
    private static int REQUEST_PERMISSION_CODE = 1;

	//循环申请字符串数组里面的权限,在小米中是直接弹出一个权限框等待用户确认,确认一次既将上面数组里面的权限全部申请
	if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(AlertActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(AlertActivity.this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
            }
        }

	

下面是使用例子:

public class AlertActivity extends AppCompatActivity {
   
    //读写权限
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    //请求状态码
    private static int REQUEST_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert);
        
        //循环申请字符串数组里面所有的权限
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
            }
        }
    }

	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
	    if (requestCode == CODE_FOR_WRITE_PERMISSION){
	        if (permissions[0].equals(Manifest.permission.REQUEST_PERMISSION_CODE)
	            &&grantResults[0] == PackageManager.PERMISSION_GRANTED){
	           Toast.makeText(context, "用户同意使用权限", Toast.LENGTH_SHORT).show();
	        }else{
	            //用户不同意,向用户展示该权限作用
	            Toast.makeText(context, "用户不同意,向用户展示该权限作用", Toast.LENGTH_SHORT).show();
	            if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
	                AlertDialog dialog = new AlertDialog.Builder(this)
	                        .setMessage("该功能需要赋予访问的权限,不开启将无法正常工作!")
	                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
	                            @Override
	                            public void onClick(DialogInterface dialog, int which) {
	                                finish();
	                            }
	                        })
	                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
	                            @Override
	                            public void onClick(DialogInterface dialog, int which) {
	                                finish();
	                            }
	                        }).create();
	                dialog.show();
	                return;
	            }
	            finish();
	        }
	    }
	}
    
 }

看不懂私聊

你可能感兴趣的:(android,权限类)