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

前端 未结 6 1948
北海茫月
北海茫月 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:42

    The stack trace seems to indicate that your permissions flow is working ok, but the call to startActivity from onRequestPermissionsResult() is crashing. Is the Intent you're passing to startActivity set correctly? I can't see it being set in that part of the code.

    Note also that ContextCompat.checkSelfPermission handles the SDK version checking on your behalf, so you should be able to use

    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
    }
    else
    {
        startActivity(intent);
    }
    

    by itself, without the wrapping SDK version check code.

提交回复
热议问题