简体   繁体   中英

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?

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.

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 (ie, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM