Android Question Android 10 services problem

ddk1

Member
Licensed User
I have a Main activity that calls a StartServiceAt. That service, if it detects that Main is paused, runs StartActivity(Main).
However, if the service is in the background, Main never gets started. I have new Android 10 mobile. It worked fine on old Android 9 mobile.
I've seen several posts on the problems with Android 10 and background services, how my app service needs to be foreground to start any other activity, and I have read the 'services lifecycle on android 8+', but still no go.

For testing, I create a timer then hit the Home button on my mobile so the app goes into background

StartServiceAt:
StartServiceAtExact(ReminderService, dtNextTimer, True)
This successfully triggers ReminderService at the appointed time.

Service_Start:
Sub Service_Start (StartingIntent As Intent)

    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    If IsPaused(Main) Then
        ' Wake phone up (so can show timers)
        Dim pws As PhoneWakeState
        pws.KeepAlive(True)
        pws.ReleaseKeepAlive
        StartActivity(Main)        'force into foreground/resume
    Else
        CallSub(Main, "UpdateTimers")
    End If
    'Service.StopAutomaticForeground
    StopService(Me)

End Sub
This runs successfully, finds Main is paused, tries to wake the phone (not relevant to this issue), then should start Main.
From what I have read, by default the service will be Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED so it should have become foreground, and so it should have been able to start Main. I tried forcing it Service.AUTOMATIC_FOREGROUND_ALWAYS but no change.
 

ddk1

Member
Licensed User
....continued

Before calling StartServiceAt, I create a persistent notification. When Main is not started by the service, I open the notification panel and click on my one, and voila the app (Main) opens.
Notification:
Dim n As Notification
n.Initialize2(n.IMPORTANCE_LOW)
n.Icon="icon"
n.OnGoingEvent = True
n.AutoCancel = False
n.Insistent = False
n.Sound = False
n.Vibrate = False
n.Light = False
n.Sound = False
n.SetInfo(Starter.gstrAppName, "Timer running", "Main")
intNotifNo = Rnd(1,999)
n.Notify(intNotifNo)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

ddk1

Member
Licensed User
I had seen that but I'm sure in another post (which I can't find now) I thought it had said there were 2 ways to fix problem. 1 was to ensure service is foreground or 2, get the overlay permission. To which the person who's post it was said the overlay example looked complicated.
And I agree, its not as simple as other permission handling eg. rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE), having to get into JavaObject stuff. So the first option, getting foreground, seemed the simplest answer.
On reading it now it seems that's not really an option for Android 10. Looks like I've got to get the hang of that overlay permission.
Edit: oh too easy. When I actually took the time to read Erels post carefully, create the class module and call it, it works perfectly and took all of 5 minutes.
Thanks.
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top