I'm trying to make an app that start a call when it receives a specific message. Everything is alright except the app crashes when it should start the call.
The problem is at the line where I use startActivity()
. I don't know why it crash. Can someone help me?
public class SmsBroadcastReceiver extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
@Override
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
//....
String cod = "message";
if(smsBody.compareTo(cod) == 0){
CallClass obj = new CallClass();
obj.call();
}
//....
private class CallClass extends AppCompatActivity {
public void call() {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+107222222"));
startActivity(callIntent);
}
}
EDIT:
Also I tried the code below:
private class CallClass extends AppCompatActivity {
public void call() {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+107222222"));
try{
startActivity(callIntent);
}
catch(SecurityException e) {
e.printStackTrace();
}
}
}
But it don't solve my problem. I checked and I gave all permissions that app needs to start a call. In debugging mode I receive this message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: romania.ovi.smsapp, PID: 9715
java.lang.RuntimeException: Unable to start receiver romania.ovi.smsapp.SmsBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3388)
....
W/System: A resource failed to call close.
Make sure you have this in your AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />
And your method should probably look like this:
public void call(String message, Context context) {
if(message.compareTo("SpecificMessage")){
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+40773733585"));
startActivity(callIntent);
}
}
The problem is that you are trying to use an Activity as just another class. As a rule of thumb, if your are calling an Activity constructor, you are doing something wrong. Either the class should not be an activity or you should not be calling the constructor.
In your use case, you would be better off writing the call code in the BroadcastReceiver itself.
@Override
public void onReceive(Context context, Intent intent) {
...
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+107222222"));
context.startActivity(callIntent);
}
Edit:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+107222222"));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.