I am trying to create an application that calls the sender of an SMS as soon as the smartphone receives an SMS. This is my code:
public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){ // ... (Managing SMS) Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + sender)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } }
But it dials and calls the sender only when the application is in foreground, while I’d like it to work always. Using the debugger, the execution flows, but it is unable to start the ACTION_CALL activity somehow. Am I missing anything? Thank you very much in advance
Advertisement
Answer
Since Android 10, there are limitations on when/how background processes (Service
, BroadcastReceiver
) can launch activities. This is your problem.
See this guide for more details.