Android Question SOLVED - DEMO App provided - Log Off User when Device Goes to Sleep

Discussion in 'Android Questions' started by tdocs2, Feb 25, 2015.

  1. tdocs2

    tdocs2 Well-Known Member Licensed User

    Greetings, all.

    I have seen a lot of threads related to the device sleep subject, but none seem to hit the mark for my purposes. My question is:

    Would the following steps accomplish the task of logging off the user in my app when the device goes to sleep?

    1. Device is going to sleep
    2. I prevent it by PhoneWakeState.KeepAlive (I assume this goes in Act Pause.)
    3. Log off user in my app
    4. Release KeepAlive

    Since I have no experience in this area, does anyone have a code snippet on this? I am certain that this requirement is not new.

    Thank you in advance for your replies.

    Sandy
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You don't need to use PhoneWakeState here. Instead use PhoneEvents in a service to monitor the ScreenOff event. Log out the user when it fires.
     
    tdocs2 likes this.
  3. tdocs2

    tdocs2 Well-Known Member Licensed User

    Thank you, Erel.
     
  4. tdocs2

    tdocs2 Well-Known Member Licensed User

    Greetings.

    I tried the PhoneEvents (zero experience in this area and weak on Services)

    This is the code (which must be wrong because it is not working):

    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Public PE As PhoneEvents
    End Sub
    Sub Service_Create
        PE.initialize(
    "PE")
    End Sub

    Sub Service_Start (StartingIntent As Intent)
      
    End Sub

    Sub Service_Destroy

    End Sub
    Sub PE_ScreenOff
        
    CallSub(Main,"ResetMain"' logs off user
      
    End Sub
    Any help will be appreciated.

    Sandy
     
    Last edited: Feb 27, 2015
  5. Erel

    Erel Administrator Staff Member Licensed User

    The activity will most probably be paused when the event fires. You need to log the user out with service code.

    You can also use CallSubDelayed to call the Main activity. The message will be queued until the activity is resumed.
     
    Peter Simpson and tdocs2 like this.
  6. tdocs2

    tdocs2 Well-Known Member Licensed User

    Thank you, Erel.

    I will make changes and test some more.

    Best regards.

    Sandy
     
  7. tdocs2

    tdocs2 Well-Known Member Licensed User

    IT WORKS! Thanks, again, Erel.

    I attach small demo project for those who want to apply this concept. See documentation for PhoneEvents for all events detected by the service.
     

    Attached Files:

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