Android拨打电话动态获取权限的问题。

做Android拨打电话时,经常会发生许多问题。

即使在AndroidManifest.xml下注册权限:

<uses-permission android:name="android.permission.CALL_PHONE" />

还是会报错。这是因为在android6.0之后即使获取权限也还是要动态获取,本人英文比较差,有查到官方文档的希望能评论一下我,谢谢。

下面是我的解决办法:

我写了一个打电话的方法call_phone,当点击按钮时会调用这个方法,我在这里贴出我的call_phone代码:

public void call_phone(String phone) {

    if (ContextCompat.checkSelfPermission(this,Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
        //首先判断否获取了权限
        if (ActivityCompat.shouldShowRequestPermissionRationale( this,Manifest.permission.CALL_PHONE)) {
            //让用户手动授权
            Toast.makeText(this, "请授权!", Toast.LENGTH_LONG).show();
            
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", getPackageName(), null);
            intent.setData(uri);
            startActivity(intent);
        }else{
            ActivityCompat.requestPermissions( this,new String[]{Manifest.permission.CALL_PHONE},1);
        }
    }else {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + phone));
        startActivity(intent);
    }


}
这样拨打电话时就不会出错了



猜你喜欢

转载自blog.csdn.net/dq1996928/article/details/80527515