Android Question Service not running on sleepmode

AHilberink

Active Member
Licensed User
Longtime User
Hi,

I do have trouble getting my service to work on Andriod 8.0.0 and B4A v8.50.

I need to check every 2 minutes. This works fine with App visible. Also with App visible and Phone still alive, but pressing the power button this doesn't work anymore.

Part of my code:
B4X:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    
    Service.AutomaticForegroundMode=Service.AUTOMATIC_FOREGROUND_ALWAYS
End Sub

Sub Service_Start (StartingIntent As Intent)
    'schedule the service to start every 2 minutes. Note that StartAtBoot is set to true
    StartServiceAtExact(Me, DateTime.Now + 2 * DateTime.TicksPerMinute, True) 'om de 2 minuten
    If(EigenFuncties.Internet=True) Then OphalenMessages
    Service.StopAutomaticForeground
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
.........

Do I need more, like a Phonewakestatus/keepalive or something?

I read a lot of chats, but cannot find what I was looking for.

Can somebody help?

Best regards,
André
 

AHilberink

Active Member
Licensed User
Longtime User

Thanks for your reply.

I tried to get it work. It did as long as press the power button within the App. If I use the back button, go to another App and press the power button there it don't work. No notify/vibration and no Log report.

I hope I descripted it correctly. Let me know if you need more info.

Best regards,
André
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Hi Erel,

Thanks for your reply.

1. You should use a different service for this. The starter service should not be a foreground service and should not start at boot.

I changed both settings, no change of result. With "different" you mean a different kind or do you mean two different services?

2. You need to acquire a partial lock if you want the CPU to keep running when the device sleeps. It is likely that the app will be paused at some point even with a wake lock.

I put the following code into the main module:
B4X:
    If FirstTime Then
        Dim pws As PhoneWakeState
        pws.KeepAlive(False)
        pws.PartialLock
    End If

Is this what you mean? No change of result.

Current status:
Within App pressing Power Button, does still run the service. Back button and go to another App and pressing Power Button, the service is stopped/even the whole app is killed. My tablet using an older version of android works fine.

Someone has same experience or more suggestions to try?

Best regards,
André
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Thanks for your reply.

I tried to get it work. It did as long as press the power button within the App. If I use the back button, go to another App and press the power button there it don't work. No notify/vibration and no Log report.

I hope I descripted it correctly. Let me know if you need more info.

Best regards,
André

I do following experiment on my phone (API 8.1). I start test app, switch to webbrowser, power off. After 15 seconds my app appears again.
The "secret" of sucess are two lines in manifest
B4X:
SetActivityAttribute (main, android:showWhenLocked, "true")
SetActivityAttribute (main, android:turnScreenOn, "true")
These attributes were added in API 27.

Which android.jar do you use ? Probably there is a reason to use Platform 27 or 28

Also there is a reason to look phone settings (probably they prevent wake up - there are very exotic firmwares)
 
Last edited:
Upvote 0
Top