Permanent services on android oreo

前端 未结 2 976
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 02:39

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 suggest

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 02:57

    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.

提交回复
热议问题