随记7——Android6.0动态权限申请

    xiaoxiao2021-03-31  33

    女神镇楼

    Android6.0之后,增强了权限管理,有些权限(比如拍照、打电话等)只在AndroidManifest.xml中声明已经不可以了,需要动态申请。以下就以打电话为例看一看如何动态申请6.0的打电话权限。

    如下:

    1、先检查权限,没有的话请求权限

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 100); return; } Intent intent_5 = new Intent(Intent.ACTION_CALL, Uri.parse("tel:123456789")); startActivity(intent_5);2、根绝返回结果处理 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 100 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Intent intent_5 = new Intent(Intent.ACTION_CALL, Uri.parse("tel:123456789")); startActivity(intent_5); }else{ Log.e("aaa", "没有打电话权限"); } }就是这样。白了个白!

    转载请注明原文地址: https://ju.6miu.com/read-665427.html

    最新回复(0)