Android Tutorial Automatic Foreground Mode

Discussion in 'Tutorials & Examples' started by Erel, Mar 12, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This is a new feature added in B4A v8.0.

    Starting from Android 8.0, services cannot be simply started while the app is in the background or not running at all. Background means that there isn't any visible activity or any service in foreground state (Service.StartForeground).

    Common cases where we expect services to start even if the app is not in the foreground:

    - Push notifications
    - Services scheduled with StartServiceAt or StartServiceAtExact
    - Services set to start after boot with #StartAtBoot attribute
    - Home screen widgets

    The only way for services to start when the app is not in the foreground is with a new API that ensures that the service will start and immediately switch to foreground mode.
    This is now handled automatically in the B4A framework. When needed services automatically start in foreground mode.
    Needed = Running on Android 8+ and the app is considered in the background when the service is started.

    You need to do one thing which is to stop the automatic foreground mode when the service completed its task.
    This can be done in two ways:

    1. Call Service.StopAutomaticForeground. This will stop the automatic foreground mode if the service was in that mode. Otherwise it will not do anything.
    It is safe to call it multiple times. The service will not be immediately destroyed.

    2. Call StopService(Me).

    If you don't stop the automatic foreground mode then a notification icon will be left after the task completed.

    For example if you are receiving push notifications:
    Sub Service_Start (StartingIntent As Intent)
    0'allow the MessageReceived event to be raised.
    End Sub
    Sub Service_Start (StartingIntent As Intent)
    End Sub
    Note that the foreground icon will not be visible at all when the task completely almost immediately.

    Service.AutomaticForegroundMode - This is a new property that you can set in Service_Create to override the default behavior.
    The possible values are:

    Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED - This is the default value. The service will enter foreground mode when needed as described above.

    Service.AUTOMATIC_FOREGROUND_NEVER - It will never enter foreground mode. This means that you must call Service.StartForeground when needed or your app will crash.

    Service.AUTOMATIC_FOREGROUND_ALWAYS - Service will always start in foreground mode, on all versions of Android. This can be useful if you want to make sure that the app is not killed until the task completes. Note that you should also acquire a partial wake lock.

    Another new property is Service.AutomaticForegroundNotification. This allows you to change the notification that appears in automatic foreground mode.
  2. kisoft

    kisoft Member Licensed User

  3. Erel

    Erel Administrator Staff Member Licensed User

    This is indeed a case where the service will start from the background.
    Add Service.StopAutomaticForeground before the end of Service_Start.
  4. kisoft

    kisoft Member Licensed User

    I will ask if it is good
    Sub Service_Start(startingIntent As Intent)
    If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
    Dim messages() As Message
            messages = ParseSmsIntent(startingIntent)
    For i = 0 To messages.Length - 1
    End If
    End Sub
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. kisoft

    kisoft Member Licensed User

    Thank you.
  7. Alessandro71

    Alessandro71 Member Licensed User

    should we add
    to our Starter service in existing project?
  8. Erel

    Erel Administrator Staff Member Licensed User

    It will not do any harm though it is not needed. The starter service is never started from a receiver.
  9. johndb

    johndb Active Member Licensed User

    I ran some tests with B4A 8.0 Beta and can confirm that FCM works well in background using a target SDK 27 where it had failed (crashed) using 7.8 in the past (Android 8.1.0). Thank you!
    Last edited: Mar 13, 2018 at 3:21 PM
    Multiverse app and Erel like this.
  10. Erel

    Erel Administrator Staff Member Licensed User

    Good. For now I recommend you to set the targetSdkVersion to 26.
    johndb likes this.
  11. rscheel

    rscheel Well-Known Member Licensed User

    If the applications are updated with this, will they continue to work with an Android version lower than 8.0?
  12. johndb

    johndb Active Member Licensed User

    Yes, they will.
    rscheel likes this.