How to request permission to make an Android phone call from Android Marshmallow?

I am trying to make a phone call with Android, and I have also set runtime permissions. And he asks whether to allow phone calls. But when I click allow, the application crashes:

Here's how I implemented it:

private static final int REQUEST_PHONE_CALL = 1; Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+918511812660")); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 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); } } else { startActivity(intent); } 


 @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_PHONE_CALL: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { startActivity(intent); } else { } return; } } } 

This is what I get in logcat:

 java.lang.RuntimeException: Failure delivering result ResultInfo{ who=@android :requestPermissions:, request=1, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.devpost.airway/com.devpost.airway.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString()' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:3733) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString()' on a null object reference at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485) at android.app.Activity.startActivityForResult(Activity.java:3930) at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75) at android.app.Activity.startActivityForResult(Activity.java:3890) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856) at android.app.Activity.startActivity(Activity.java:4213) at android.app.Activity.startActivity(Activity.java:4181) at com.devpost.airway.activities.MainActivity.onRequestPermissionsResult(MainActivity.java:140) at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6582) at android.app.Activity.dispatchActivityResult(Activity.java:6460) at android.app.ActivityThread.deliverResults(ActivityThread.java:3729) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

What could be the reason for this?

+5
source share
5 answers

The stack trace seems to indicate that the permission flow is working fine, but the startActivity call from onRequestPermissionsResult() failing. Is the Intent parameter set to startActivity ? I do not see it being installed in this part of the code.

Note that ContextCompat.checkSelfPermission handles 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); } 

independently, without a verification code for the SDK version for packaging

+12
source

You need to create an Intent in onRequestPermissionsResult

 @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_PHONE_CALL: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+918511812660")); startActivity(intent); } else { } return; } } } 
+5
source

add new method

 public static boolean hasPermissions(Context context, String... permissions) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) { for (String permission : permissions) { if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { return false; } } } return true; } 

add this to Global

 int PERMISSION_ALL = 1; String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE}; 

and write below code in onCreate

 if(!hasPermissions(this, PERMISSIONS)){ ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL); } 
0
source

Change the onRequestPermissionsResult below, you need to create intent first and then call it with permission. What you do is 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); } } } } } 
0
source

I would prefer to use ACTION_DIAL instead of ACTION_CALL when building an Intent to call a specific number. Using ACTION_DIAL, you do not need any permissions for calls in your application, since ACTION_DIAL opens the dialer number with the number already entered and additionally allows the user to decide whether to make a call or change the phone number before making or not making calls.

 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "Your Phone_number")); startActivitLy(intent); 
0
source

Source: https://habr.com/ru/post/1258453/


All Articles