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

Discussion in 'Android Questions' started by bjfhs, Aug 5, 2019.

  1. bjfhs

    bjfhs Active Member Licensed User

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

    Attached Files:

  2. Didier9

    Didier9 Active Member Licensed 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...
     
  3. ac9ts

    ac9ts Active Member Licensed User

  4. bjfhs

    bjfhs Active Member Licensed User

    Wait for your code.
     
  5. bjfhs

    bjfhs Active Member Licensed User

  6. Didier9

    Didier9 Active Member Licensed 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:

    Code:
    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.
     
    emexes likes this.
  7. bjfhs

    bjfhs Active Member Licensed User

    Thank you very much,but this is not I want ,I need the app keep alive when screen off.
     
  8. bjfhs

    bjfhs Active Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice