Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permanent services on android oreo

Android 8's battery consumption improvements are nice to the user but I am a bit afraid if my service will work as expected.

First of all: Thank you for any suggestions but I cannot just schedule my service. I want to make a OK Google-like keyword listener running in the background all the time. It will be based on the open source pocketsphinx-android library. I know that this will consume much battery power and I will inform the user about this.

Can we create a permanent background service on android 8+ ? I need to target android 8 in gradle because I was expecting some bugs with older targets. I also don't want to annoy a user with a foreground service which permanently shows a notification in the status bar.

[https://developer.android.com/about/versions/oreo/background.html] - Is there really no way of making permanent background services for my use-case (but preferably for all use-cases) possible?

like image 782
fameman Avatar asked Mar 08 '23 14:03

fameman


2 Answers

Unfortunately, it's not possible to use a background service and don't show a foreground notification on Android 8.0 and higher.

The only one way that it might work is if you stick your app to Google APIs such as Voice Actions API.

As far as I know there is no a good work around and most apps like WhatsApp are still targetting Android API 24.

like image 76
andrei_zaitcev Avatar answered Mar 10 '23 11:03

andrei_zaitcev


For what is worth, I am sharing me experience on that:

It's partially possible to use a background service and not showing a foreground notification on Android 8.0 just made some experiments and ended up with this:

Just create a notification channel with IMPORTANCE_NONE, and the notification will still exist, but will not be displayed in status bar.

Code excerpt:

NotificationChannel channelService = new NotificationChannel(
    YOUR_SERVICE_CHANNEL_ID,
    YOUR_CHANNEL_HUMAN_READABLE_NAME,
    NotificationManager.IMPORTANCE_NONE);

channelService.setSound(null, null); // let's be quiet : no sound
channelService.setLockscreenVisibility(Notification.VISIBILITY_SECRET); // not on lock screen

// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
// changes needs to uninstall app or clean app data, or rename channel id!
notificationManager.createNotificationChannel(channelService));


This way, the notification will not be displayed when your app is running in foreground.

Not a perfect solution:

When your app goes in background (i.e., you open another app), the "Android system" app displays a notification about "(your) app being running in the background". So, this is not great, but, from my point of view, it's a bit better than before.

like image 30
Pascal Avatar answered Mar 10 '23 11:03

Pascal