How to create background services for android Oreo without notifications like social networking applications Facebook, Messenger, Zalo

后端 未结 4 867
轻奢々
轻奢々 2021-01-28 16:05

I see some applications like Messenger, Zalo do not need notifications but still work in the background. How to do it. The devices I use are Xiaomi A1 (Android 8) and Nokia 6.1

相关标签:
4条回答
  • 2021-01-28 16:15

    No, you cannot start a service to work in the background when app is not in the foreground without showing the foreground notification API >= Oreo. I suggest you use WorkManager, a convenient lib for background tasks without foreground notification. Check it out there: Work manager

    0 讨论(0)
  • 2021-01-28 16:18

    Starting with Android 6.0 (API level 23), Android introduces two power-saving features that extend battery life for users: DOZE and APP STANDBY. These two features enforce many restrictions on your background processing while the phone is in Doze mode. You should read about Doze and app standby in the following link

    https://developer.android.com/training/monitoring-device-state/doze-standby

    Now, about your use case is that you want to receive the messages and incoming calls even when the app is not running. For this use case, Android announced High Priority FCM messages in GoogleIO2016. They are high priority Push message which grant the application temporary wakelock and network access, independent of Device's Doze state or if the app happens to be in the app standby. This allows the application to react to the message and notify the user in whatever way it wants about the instant message or incoming call.

    For more about your use case, follow the below GoogleIO2016 Video from 08:30m to 10:30m https://www.youtube.com/watch?v=VC2Hlb22mZM&t=505s

    and read about this use case on the first link in this answer.

    0 讨论(0)
  • 2021-01-28 16:22

    You can using Service class.

    Refer this link, which I found on so: How to make an android app to always run in background?

    Fix me if I have any wrong! Hope it can help you.

    0 讨论(0)
  • No. For API levels of Oreo (i.e.: 26) or higher, you cannot start a service to work in the background when the app is not in the foreground without showing the foreground notification. One alternative is to use WorkManager, a convenient lib for background tasks that doesn't require a foreground notification. Check it out here: WorkManager

    The caveat with WorkManager though is that it does not allow you to execute tasks more frequently than once every 15 minutes. So if you need some background task to run every 30 seconds, 5 minutes, etc., you'll need to explore other options instead.

    0 讨论(0)
提交回复
热议问题