startForegroundService() does not start service on 2 Samsung devices

185 views Asked by At

The code to start this service now looks like

if (!BuildConfig.MSG_ALLOWED && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  Log.v("Foreground SmsService Launch");
  context.startForegroundService(intent);
} else {
  Log.v("Regular SmsService Launch");
  context.startService(intent);
}

The log statements were, unfortunately, not included in the version that my two users were using, but indirect evidence is pretty convincing that the proper conditions were satisfied to make the startForegroundService() calls. For most users, this works fine, but for these two users, the requested service never starts up. No exception are thrown, the system log reports

W ActivityManager: Background start not allowed: service Intent { act=net.anei.cadpage.Telephony.SMS_RECEIVED flg=0x19000010 cmp=net.anei.cadpage/.SmsService (has extras) } to net.anei.cadpage/.SmsService from pid=10201 uid=10234 pkg=net.anei.cadpage startFg?=true

Both users are running Android 9 on Samsung devices, which is starting to look suspicious. Any suggestions on what I can do to fix things?

Since someone is probably going to ask why the heck and I starting foreground services from a background receiver, the answer is that I am processing an incoming text message which would normally be exempt from the these background service start limitations. But the Android system does not know that I am processing incoming text messages because regular apps distributed through the Play Store are not allowed to do that. My users have to sideload an auxiliary app to monitors incoming text messages and passes them on to the regular app.

1

There are 1 answers

1
sdex On

There was an issue on android 9 with starting a foreground service.
In case, when the activity isn't fully resumed startForegroundService throws an exception.
To make sure you can start the foresgound service you need to check if your app has IMPORTANCE_FOREGROUND importance and only after that start the service otherwise postpone it.