You can use foreground service and register your receiver in your foreground service instead of manifest. Using a foreground service may reduce delay as foreground service is more important than background service and registered receiver in a foreground service may be more important to OS than registered receiver in manifest as always your program is running and in foreground before phone state is changed.
Also i do not think ACTION_PHONE_STATE_CHANGED
is an Ordered Broadcast as it could not be aborted by receiver thus using Priority which other answers suggest has no effect on it (priority is only for ordered broadcasts).
remove phone state receiver from manifest but keep your BOOT_COMPLETED receiver in manifest and start following service at boot complete
register receiver in onCreate of a Service:
@Override
public void onCreate() {
super.onCreate();
phoneStateReceiver = new PhoneStateReceiver();
registerReceiver(phoneStateReceiver, new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
}
unregister in onDestroy:
@Override
public void onDestroy() {
unregisterReceiver(phoneStateReceiver);
super.onDestroy();
}
add a static method to your service to start service:
// start service even if your app is in stopped condition in android 8+
static void requestStart(@NonNull final Context context, @NonNull final String action){
final Context appContext = context.getApplicationContext();
Intent intent = new Intent(appContext, AppService.class);
intent.setAction(action);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// this is required to start the service if there is
// no foreground process in your app and your app is
// stopped in android 8 or above
appContext.startForegroundService(intent);
} else {
appContext.startService(intent);
}
}
start foreground in your onStartCommand
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(ACTION_START.equals(intent.getAction()))
startForeground(ID, notification);
else if(ACTION_STOP.equals(intent.getAction()))
stopForeground(true);
return START_STICKY;
}