Android Question Get data from sensors when the screen is off (Solved)

bjfhs

Active Member
Licensed User
Longtime User
I want get data from PhoneAccelerometer when the screen is off,but I can't get anything.
My code in the attached.
 

Attachments

  • test.zip
    9.4 KB · Views: 123

Didier9

Well-Known Member
Licensed User
Longtime User
Typically, when the screen turns off the activity is suspended. To keep some process running you need to prevent Android from suspending your activity. This will prevent the screen from turning off. You can manually turn the screen off to make it look like your app has stopped while keeping it running.

The activity will still stop if you start another app or if you switch to another activity within your app. To keep something running in the background even when other apps are running, you need to put your code in the Starter module as it is the only module that you can keep running in the background while other activities are on the screen (at least this is my understanding).

I do have some code that does that but no time to post it now. Maybe later today or tonight...
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

bjfhs

Active Member
Licensed User
Longtime User
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.
Thank you very much,but this is not I want ,I need the app keep alive when screen off.
 
Upvote 0
Top