Search code examples
androidbroadcastreceiver

start phone call activity through BroadcastReceiver crashes the calling app


In my BroadcastReceiver's onReceive() function I call the following code to make a phone call without user intervention with following code:

    Intent intentcall = new Intent();
    intentcall.setAction(Intent.ACTION_CALL);
    intentcall.setData(Uri.parse("tel:" + phoneNumber)); 
    startActivity(intentcall);

The above code works when placed in the onCreate function of the calling activity, but fails in the BroadcastReceiver's onReceive() function. The BroadcastReceiver is an inner class inside the calling activity.

The following are errors:

    11-29 15:15:40.822: E/AndroidRuntime(6354): FATAL EXCEPTION: main 11-29 15:15:40.822: E/AndroidRuntime(6354): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000010 (has extras) } in com.ensil.vastusyscontrolunit.OutgoingReceiver@43d3d4e8 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:781) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.os.Handler.handleCallback(Handler.java:725) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.os.Handler.dispatchMessage(Handler.java:92) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.os.Looper.loop(Looper.java:137) 11-29 15:15:40.822: E/AndroidRuntime(6354): at android.app.ActivityThread.main(ActivityThread.java:5328) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at java.lang.reflect.Method.invokeNative(Native Method) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at java.lang.reflect.Method.invoke(Method.java:511) 11-29 15:15:40.822: E/AndroidRuntime(6354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at dalvik.system.NativeStart.main(Native Method) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): Caused by: java.lang.NullPointerException 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.widget.Toast.(Toast.java:105) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.widget.Toast.makeText(Toast.java:261) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at com.ensil.vastusyscontrolunit.OutgoingReceiver.onReceive(OutgoingReceiver.java:25) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:771) 
    11-29 15:15:40.822: E/AndroidRuntime(6354): ... 9 more

Solution

  • you must use your code like below code in onReceive():

    Intent intentcall = new Intent();
    intentcall.setAction(Intent.ACTION_CALL);
    intentcall.setData(Uri.parse("tel:" + phoneNumber)); 
    intentcall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    context.startActivity(intentcall);  
    

    did you use Toast in your code? and how you call that because the Toast makes your app crashing, comment that line and try again or post that to help you.