Waking from sleeping mode -> prompt for password

Yafuhenk

Active Member
Licensed User
Longtime User
Hi,

my app is password protected.
When the device get out of the sleeping mode is should prompt for a password again.
The problems is that I don't know how to do this?
Is there a way to detect "waking up"?

Thanks for any idea / suggestions

Henk
 

Yafuhenk

Active Member
Licensed User
Longtime User
Yes I know but it is also called when going from one activity back to the other. This doesn´t work but thanks anyhow for the feedback

Henk
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You didn't write if you are using multiple activities or not.
PhoneEvents will not really help you as eventually the process will be killed together with the listener.

You should write the current time in Activity_Pause of each activity and then check it in Activity_Resume (use a class or code module for that). If more than x seconds passed you should show the password screen.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You didn't write if you are using multiple activities or not.
PhoneEvents will not really help you as eventually the process will be killed together with the listener.

You should write the current time in Activity_Pause of each activity and then check it in Activity_Resume (use a class or code module for that). If more than x seconds passed you should show the password screen.

Or perhaps you could use a proccess variable boolean flag set to true when passing from one activity to another. For e.g. you may write something like activity_change_flag=true.
Then, when activity_Resume is executed, you should set activity_change_flag=false and do nothing (apart of course from any other code you wish to use inside the activity_resume routine). Then, when you are still in the same activity and device gets back from sleep, activity_resume will be triggered, and thus you can show up the password box, by simply checking whether activity_resume is set to false. something like this:
B4X:
Sub Process_Globals
   Dim activity_change_flag As Boolean
        activity_change_flag=false
End Sub
Sub Activity_Resume
   If activity_change_flag=False Then
      ' this means we arrive here when device wakes up from sleep mode
      ' open the password box
   End If
   activity_change_flag=False
   ' do stuff you normally do when this event is triggered
   ' ........
   ' this thing will not work correctly, IF activity_resume is triggered even when in the same activity, without device going to sleep
End Sub
 
Upvote 0

Yafuhenk

Active Member
Licensed User
Longtime User
Thanks for the feedback

I just solved it as follows:

B4X:
'this requires the Reflection library
'checks if screen is on, that is not in sleep modus
'This code is in a code module called Screenstate
Sub IsScreenOn As Boolean
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "power", "java.lang.String")
   Return r.RunMethod("isScreenOn")
End Sub

'Use this in the main activity
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim ScreenOn, PromptForPassword As Boolean
End Sub

Sub Activity_Resume
   If PromptForPassword = True Then
      'Write your code for password check
      PromptForPassword = False
   End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   ScreenOn = ScreenState.IsScreenOn
   If ScreenOn = False Then
      PromptForPassword = True
   End If
End Sub

'Use this in every sub activity
Sub Activity_Pause (UserClosed As Boolean)
   Main.ScreenOn = ScreenState.IsScreenOn
   If Main.ScreenOn = False Then
      Main.PromptForPassword = True
      Activity.Finish 'go to main
   Else
      Main.PromptForPassword = False
   End If
End Sub

N.B. My app uses Landscape mode only. I expect that if an app uses both portrait and landscape the app will prompt for a password when the orientation changes. But as said this isn't a problem for me in this app.

Thanks

Henk
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several problems with your code.
What will happen if the user presses on the Home button (or moves to any other app) and then turns off the screen?

You should also expect the OS to eventually kill the process. In that case PromptForPassword will be False when the process is recreated. You should at least set it to True in Sub Process_Globals.
 
Upvote 0

Yafuhenk

Active Member
Licensed User
Longtime User
Hi Erel,

You were right it didn't work when pressing the home button or switching to another app and then return.

I solved the issue as suggested by you in #6

Thanks

Henk
 
Upvote 0
Top