Reputation: 161
I have written and published an app ("Sensor Recording") in the Google Play Store. It's about reading sensor data (such as position, accelerometer, gyroscopes, etc.), displaying them numerically or graphically and storing them into kml and csv files for further processing, e.g with Google Earth or MS Excel. I have established a service to read and process the data in the background even when the screen is switched OFF.
Everything was working fine until Android 8. But in Oreo, the service is stopped automatically by the operating system, approx. 5 minutes after the screen is switched OFF. This has been introduced by Google intentionally to save battery lifetime. I have found some measures in the internet which should avoid that, but nothing worked so far.
What I have done:
1.) in the calling activity I have replaced startService()
with startForegroundService()
2.) in the service itself I have made some modifications in onStartCommand()
according to the hints I have found.
Tests with wakeLock
also led to nothing. Any further ideas are appreciated.
private NotificationManager notMan;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// show notification + startForeground
int id = 42;
String channelId = "42";
String text = "Sensors active";
Intent notificationIntent = new Intent(this, SensorService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel(channelId,
getString(R.string.app_name),
NotificationManager.IMPORTANCE_DEFAULT);
notMan = getSystemService(NotificationManager.class);
notMan.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(this, channelId);
builder.setSmallIcon(R.drawable.vector3d_bg_transp)
.setContentTitle("Sensor Service")
.setContentText(text)
.setTicker(text)
.setSubText("Start Service")
.setShowWhen(true);
notification = builder.build();
}
else
{
notMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.vector3d_bg_transp)
.setContentTitle("Sensor Service")
.setContentText(text)
.setTicker(text)
.setSubText("Start Service")
.setPriority(PRIORITY_DEFAULT)
.setShowWhen(true).build();
}
startForeground(id, notification);
return super.onStartCommand(intent, flags, startId); // = 1, same as START_STICKY
} // onStartCommand
Upvotes: 2
Views: 362
Reputation: 161
Recently, I found the solution – after an essential hint by Thomas Künneth (author of various Android books). The remedy is not in the source code, but in the settings of the smartphone. There is an option to enable background processing. On my Huawei P10 Lite with Android 8.0 it is located in the following menu tree (probably other devices or android versions have similar options):
That’s it, quite easy – if you know how.
It is remarkable, that Google offers this option, but does not highlight it in lectures about Android 8. Of course, this is consistent with the new policy “Battery first”.
Upvotes: 1