Wait for your code.I do have some code that does that but no time to post it now. Maybe later today or tonight...
Thank you,I will try it.Try using a foreground service.
https://www.b4x.com/android/forum/threads/automatic-foreground-mode.90546/#content
Sub Process_Globals
Dim pe As PhoneEvents
Public pws As PhoneWakeState
End Sub ' Process_Globals
Sub Activity_Create( FirstTime As Boolean )
If FirstTime Then
pe.Initialize( "PhoneEvent" )
End If
End Sub ' Activity_Create()
Sub Activity_Resume
pws.KeepAlive( True )
End Sub ' Activity_Resume
Sub Activity_Pause( UserClosed As Boolean )
pws.ReleaseKeepAlive
End Sub ' Activity_Pause()
Sub PhoneEvent_BatteryChanged( Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent )
PluggedStatus = Plugged
ChargeLevel = Level
If PluggedStatus = False And ChargeLevel < 50 Then
pws.KeepAlive( False )
Else
pws.KeepAlive( True )
End If
End Sub ' PhoneEvent_BatteryChanged()
Thank you very much,but this is not I want ,I need the app keep alive when screen off.Put this in your activity to keep it alive as long as the battery is > 50% or the phone's charger is plugged in:
B4X:Sub Process_Globals Dim pe As PhoneEvents Public pws As PhoneWakeState End Sub ' Process_Globals Sub Activity_Create( FirstTime As Boolean ) If FirstTime Then pe.Initialize( "PhoneEvent" ) End If End Sub ' Activity_Create() Sub Activity_Resume pws.KeepAlive( True ) End Sub ' Activity_Resume Sub Activity_Pause( UserClosed As Boolean ) pws.ReleaseKeepAlive End Sub ' Activity_Pause() Sub PhoneEvent_BatteryChanged( Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent ) PluggedStatus = Plugged ChargeLevel = Level If PluggedStatus = False And ChargeLevel < 50 Then pws.KeepAlive( False ) Else pws.KeepAlive( True ) End If End Sub ' PhoneEvent_BatteryChanged()
This will not prevent your app from stopping if another activity becomes active so it may not be exactly what you are looking for, but I am using that code for an app where I want the activity and the screen to stay on unless the user specifically closes it.