We adjusted our ongoing notification for Oreo and it worked great. Now, on Pie only (not happening on Oreo devices), we\'re getting the titled error. Has something changed in
I stopped to get the error after implementing below code in ONCREATE
Intent intent1 = new Intent(this, YourActivity.class);
try {
startService(intent1);
}catch ( Exception e1){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.startForegroundService(intent1);
}else {
Crashlytics.log("crash for first time, trying another.");
this.startService(intent1);
}
}
For me sounds strange but is working fine without crash.
Has something changed in foreground services in Pie that I'm missing?
YES Have a look here migration notes of Android 9 / Pie
Change
Foreground service permission
Summary
Apps wanting to use foreground services must now request the FOREGROUND_SERVICE permission first. This is a normal permission, so the system automatically grants it to the requesting app. Starting a foreground service without the permission throws a SecurityException.
UPDATE
related issue in Google Issue Tracker Context.startForegroundService() did not then call Service.startForeground
As mentioned here Background Service Limitations, the app/service has five seconds to call startForeground()
, If the app does not call startForeground() within the time limit, the system stops the service and declares the app to be ANR.
There are some possibilities:
startForeground()
method.onCreate
method will not be called, instead onStartCommand
will be called. Then move your logic to onStartCommand
to call startForeground()
method.startForeground
must not be 0, otherwise it will also cause the crash.