Put this in your activity to keep it alive as long as the battery is > 50% or the phone's charger is plugged in:
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.