android android.intent.action.call crashing Android app?

1.4k views Asked by At

I am trying to make call from my app. But every time it get crashed with no error shown on logcat. I took permission in manifest also check it at runtime.

public void call(String number){
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:"+number));
    callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(callIntent);

}
2

There are 2 answers

0
Ramesh Yankati On

Are you sure context is not null ? You should do something like this. Inside your calling activity make these changes

private static final int REQUEST_CALL_PHONE_PERMISSION  =  100;


    if( isCallPhonePermissionGranted() ){
        call("<Number>");
    } else {
        call("<Number>");
    }

private void requestCallPermission() { final String[] permissions = new String[]{Manifest.permission.CALL_PHONE}; ActivityCompat.requestPermissions(this, permissions, REQUEST_CALL_PHONE_PERMISSION); }

   private boolean isCallPhonePermissionGranted() {
        return ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) 
        == PackageManager.PERMISSION_GRANTED;
     }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
           permissions, @NonNull int[] grantResults) {
             if (requestCode != REQUEST_CALL_PHONE_PERMISSION) {
                super.onRequestPermissionsResult(requestCode, permissions, 
                grantResults);
               return;
              }

         if (grantResults.length != 0 && grantResults[0] == 
                   PackageManager.PERMISSION_GRANTED) {
                   call("<Number>");
              return;
         }
   }


      public void call(String number){
         Intent callIntent = new Intent(Intent.ACTION_CALL);
         callIntent.setData(Uri.parse("tel:"+number));
         callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(callIntent);
      }

And finally add this this permission to Android Manifest.xml

         <uses-permission android:name="android.permission.CALL_PHONE" />
0
Manish Ahire On

Don't forget to add the relevant permission to your manifest:

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

An intent by itself is simply an object that describes something. It doesn't do anything.

public void call(String number){
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:" + number));
    context.startActivity(intent);
   }

And How to make call in Android 6.0 and Above