android / codelab-while-in-use-location

Codelab:
https://github.com/googlecodelabs/while-in-use-location
Apache License 2.0
84 stars 51 forks source link

Does not start in foreground in landscape mode #15

Open Taknok opened 3 years ago

Taknok commented 3 years ago

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