Android Question Using DateTime used in time doesn't triger pause event

wwregistry

Member
Licensed User
Longtime User
Here is the problem:
When using this code in a timing loop and the user presses the back arrow(thing) the program disappears but the beeping continues -- i think this is because the Activity.Pause event does not fire?

do
sleep(1000)
beep etc..
loop

----
Sub Sleep(ms As Long)
Dim Now As Long

Try

Now = DateTime.Now
Do Until ((DateTime.Now > Now + ms) )
DoEvents
Loop

Catch
End Try

End Sub
 

Mahares

Expert
Licensed User
Longtime User
I made this code and project just for you. I hope it does or come close to what you want:
B4X:
Sub Process_Globals
    Dim MyTimer As Timer
End Sub

Sub Globals
    Dim t1 As Long
    Dim MyBeeper As Beeper  'Need audio library
End Sub

Sub Activity_Create(FirstTime As Boolean)
    MyBeeper.Initialize(200, 500) '200 milliseconds, 500 hz
'    MyBeeper.Initialize(Rnd(100,301), Rnd(400,601)) 
'    MyBeeper.Initialize(Rnd(20,101), Rnd(100,201)) 'low
    MyTimer.Initialize("MyTimer",2000)  
    t1=DateTime.Now    
    MyTimer.Enabled=True  
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub MyTimer_Tick  
    Dim t2 As Long
    t2=DateTime.Now
    MyBeeper.Beep
   If t2-t1 >=20000  Then  'wait 20 sec
      MyTimer.enabled=False
     MyBeeper.Release
     Activity.Finish
   End If
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 
    If KeyCode = KeyCodes.KEYCODE_BACK Then    ' Checks if the KeyCode is BackKey
        MyTimer.Enabled=False
        MyBeeper.Release
        Activity.Finish
    End If
End Sub
 

Attachments

  • TimerAndBeeperMahares011814.zip
    6.1 KB · Views: 216
Upvote 0

TomA

Active Member
Licensed User
Longtime User
You may need to catch the 'Back' keypress to control what happens:

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
If KeyCode = KeyCodes.KEYCODE_BACK Then
*** Put code here to do what you want. If you want to control what 'Back' is doing you can handle it
*** here (such as stopping the beep). Then return True if you do not want the system to process it
*** (if you don't really want to let the system go back), otherwise return False and the system will
*** process it normally.
Else
Return False
End If
End Sub
 
Upvote 0
Top