Intent call action doesn't work on Marshmallow

后端 未结 3 1975
谎友^
谎友^ 2021-01-04 09:32

I\'m trying to start a call intent action on a device who has Marshmallow as OS, Using the same steps as usual (This is working on versions below):

Add permission:

相关标签:
3条回答
  • 2021-01-04 09:34

    Beginning in android 6.0 (API 23), dangerous permissions must be declared in the manifest AND you must explicitly request that permission from the user. According to this list, CALL_PHONE is considered a dangerous permission.

    Every time you perform an operation that requires a dangerous permission, you must check if that permission has been granted by the user. If it has not, you must request that it be granted. See Requesting Permissions at Run Time on Android Developers.

    0 讨论(0)
  • 2021-01-04 09:59

    Method to make call

    public void onCall() {
            int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
    
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        this,
                        new String[]{Manifest.permission.CALL_PHONE},
                        "123");
            } else {
                startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:12345678901")));
            }
        }
    

    Check permission

    @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode) {
    
                case 123:
                    if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                        onCall();
                    } else {
                        Log.d("TAG", "Call Permission Not Granted");
                    }
                    break;
    
                default:
                    break;
            }
        }
    
    0 讨论(0)
  • 2021-01-04 09:59

    For Marshmallow version and above you need to ask the permission at runtime not only in the manifest file. Here is the documentation:

    Requesting Permissions at Run Time

    Hope it helps.

    0 讨论(0)
提交回复
热议问题