Permanent services on android oreo

柔情痞子 提交于 2019-12-01 09:52:36

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!