How to ask permission to make phone call from Android from Android version Marshmallow onwards?

前端 未结 6 1922
北海茫月
北海茫月 2020-12-31 03:33

I am trying to make a phone call from Android, and I\'ve set run time permissions as well. And it asks whether to allow making phone calls. But when I press allow, the app c

6条回答
  •  死守一世寂寞
    2020-12-31 03:36

    Change your onRequestPermissionsResult to below, you basically need to create the intent first and then call it on permission granted. That's where you are doing it wrong.

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PHONE_CALL : {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+918511812660"));
    
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                        startActivity(intent);
                    }
                }
            }
        }
    }
    

提交回复
热议问题