codelab-while-in-use-location icon indicating copy to clipboard operation
codelab-while-in-use-location copied to clipboard

Does not start in foreground in landscape mode

Open Taknok opened this issue 4 years ago • 0 comments

Hi,

Expected: In landscape, when exiting the app, the startForeground() should be called and a notification should be displayed. On rotation screen in app, no notification should be set (which is the case).

What happen: When the smartphone is in landscape mode, the service do not start in foreground when exiting the app.

This is due to onConfigurationChanged(). Many home launcher lock screen in vertical mode. Thus, when exiting the app in horizontal mode, a screen rotation is triggered, calling the onConfigurationChanged. The var configurationChange is set to true. Thus, onUnbind does not his job even if the app is exiting:

    override fun onUnbind(intent: Intent): Boolean {
        Log.d(TAG, "onUnbind()")

        // MainActivity (client) leaves foreground, so service needs to become a foreground service
        // to maintain the 'while-in-use' label.
        // NOTE: If this method is called due to a configuration change in MainActivity,
        // we do nothing.
        if (!configurationChange && SharedPreferenceUtil.getLocationTrackingPref(this)) {
            Log.d(TAG, "Start foreground service")
            val notification = generateNotification(currentLocation)
            startForeground(NOTIFICATION_ID, notification)
            serviceRunningInForeground = true
        }

        // Ensures onRebind() is called if MainActivity (client) rebinds.
        return true
    }

ezgif com-video-to-gif

Taknok avatar Jan 21 '21 16:01 Taknok