Android Question How to runs service in background forever?

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I runs this example on my phone : Android 9, EMUI 9.1, B4A 9.8

The app is suppose to runs forever but in my case, when phone slept for a few minutes, OS killed it.
I changed the codes at service start to runs again every 3 minutes
B4X:
Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nid, CreateNotification("..."))
    StartServiceAt(Me, DateTime.Now + 3 * DateTime.TicksPerMinute, True) 'in the example it was 30'
    Track
End Sub
but no avail.

I believe it has something to do with power management in EMUI 9.1.
The default power management for all app in EMUI 9.1 are manage automatically.
If I change power management for this example application to manual and allow it to run in background, application runs as expected.

Is there a way to let service runs forever without a need to change power management's setting in the phone?
 

OliverA

Expert
Licensed User
Longtime User
Please read: https://www.b4x.com/android/forum/threads/continous-background-gps-tracking.110628/post-690743

As to power management see: https://www.b4x.com/android/forum/threads/about-runtime-permission.69703/#post-455436, but also see https://www.b4x.com/android/forum/threads/ignore-battery-optimizations.68825/post-436650. Please note, this still requires user intervention. As per the first link, Android is user becoming more and more user centric and therefore many things are just not doable without user interaction/user approval.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Please read: https://www.b4x.com/android/forum/threads/continous-background-gps-tracking.110628/post-690743

As to power management see: https://www.b4x.com/android/forum/threads/about-runtime-permission.69703/#post-455436, but also see https://www.b4x.com/android/forum/threads/ignore-battery-optimizations.68825/post-436650. Please note, this still requires user intervention. As per the first link, Android is user becoming more and more user centric and therefore many things are just not doable without user interaction/user approval.
If I understood correctly, there is no way for developers to runs foreground app forever without users approval to ignore power management for that app.

I did add Ignore Battery Optimization Dialog to user with the codes from
but even, user click OK, app in foreground still killed by OS.

So I guest, on my phone, unless users set power management manually on OS, there is no way for app to run forever in foreground.

Thanks anyway.
 
Last edited:
Upvote 0
Top