Android Question Timer stops app

bussi04

Member
Licensed User
Longtime User
I´m using B4A Version 5.80 registered version and try to get a timer running.

Timer initialization works fine. Period set is to 5000 ms. But for 1000 or 100 it´s the same.

Running the app displays first message box and after clicking Ok it takes 5 seconds until app ends without displaying specific error information.
Running the app and not clicking OK at first message box does not end the app until OK is pressed and then app is stopped after one more second. Using FirstTime flag statement makes no difference.

Error occurs on Android 5.0 and 4.4.4 .

My simple code:

code >>>
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Timer1 As Timer
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("hello")
Timer1.Initialize("Timer1_Tick",5000)
Timer1.Enabled=True
Msgbox("past Timer1 init","")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Timer1_Tick
Msgbox("Timer1_Tick","")
End Sub
<<< code

Any idea?
 

bussi04

Member
Licensed User
Longtime User
Don't use Msgbox("Timer1_Tick","") with Timers !
Msgbox are modal dialogs which hold the main thread.

Use the Logs instead.

Thx Klaus,
I know. Of course the error is the same when I don´t use those MsgBox commands.
My Asus Zenfone 2 is not rooted so I can´t watch all log entries.

But - that way I definitely know that app is stopped when Timer1 tries to enter Timer1_Tick because there 2nd MsgBox modal dialog is never reached ...

... One more detail info: Asus Zenfone 2 uses an Intel µC with Android 5.0 but error is the same on Samsung T-113 (ARM?) with Android 4.4.4.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Have you tried to replace both Msgbox lines with Logs ?
My Asus Zenfone 2 is not rooted so I can´t watch all log entries.
What do you mean with this ?
Logs are displayed in the Logs Tab.

Try this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("hello")
    Timer1.Initialize("Timer1_Tick",5000)
    Timer1.Enabled=True
    Log("past Timer1 init")
End Sub

Sub Timer1_Tick
    Log("Timer1_Tick")
End Sub
 
Upvote 0

bussi04

Member
Licensed User
Longtime User
Have you tried to replace both Msgbox lines with Logs ?

What do you mean with this ?
Logs are displayed in the Logs Tab.

Try this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("hello")
    Timer1.Initialize("Timer1_Tick",5000)
    Timer1.Enabled=True
    Log("past Timer1 init")
End Sub

Sub Timer1_Tick
    Log("Timer1_Tick")
End Sub

btw ...
I did. Because I didn´t know how to watch those log entries i googled and found out that i have to download an android app. And they said that it needs root rights to watch all entries from Android 4.1 on.
I tried CatLog and it showed a warning that it needs root access and that i only can get CatLog entries but no others.

What app do you use without root that shows your log entries?

However the error occurs without any log or MsgBox statements ...
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
No need for an external App !
In the lower right corner of the IDE you have Tabs.
One of these is the Logs Tab where the Logs are displayed.

upload_2016-6-3_13-23-15.png


You may have a look at following chapters in the Beginner's Guide:
4.3 Tabs
4.3.7 Logs
13 Debugging
 
Upvote 0

bussi04

Member
Licensed User
Longtime User
No need for an external App !
In the lower right corner of the IDE you have Tabs.
One of these is the Logs Tab where the Logs are displayed.

View attachment 44624

You may have a look at following chapters in the Beginner's Guide:
4.3 Tabs
4.3.7 Logs
13 Debugging

That was helpful for a first step! Thx once more, Klaus :)
Now I have exchanged MsgBox by Log, 3 logs in total: 1st right before Timer1.Initialize, 2nd right after Timer1.Enabled=True and 3rd in Sub Timer1_Tick. And I have added FirstTime flag.

I had to deinstall CatLog to get log entry output. But after "beginning of main" none of my log entries are logged ... ?!?
"Setting install_non_..." is displayed right after app has stopped ...
Irritating ...

code >>>
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Timer1 As Timer
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime = True Then
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("hello")
Log("before Timer1 init")
Timer1.Initialize("Timer1_Tick",5000)
Timer1.Enabled=True
Log("past Timer1 init")
End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Timer1_Tick
Log("Timer1_Tick")
End Sub
<<< code
 

Attachments

  • log1.jpg
    log1.jpg
    102.3 KB · Views: 188
Upvote 0

klaus

Expert
Licensed User
Longtime User
Can you post your project as a zip file (IDE menu File/Export As Zip).

When you insert code in a post you should use:
upload_2016-6-3_15-58-34.png


Your FirstTime test is wrong:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime = True Then
        Log("past Timer1 init")
        Timer1.Initialize("Timer1_Tick",5000)
    End If
    Activity.LoadLayout("hello")
    Timer1.Enabled=True
    Log("past Timer1 init")
End Sub

Sub Timer1_Tick
    Log("Timer1_Tick")
End Sub

Did you check Unknown sources in Lock screen and security in the Settings on your device ?
 
Upvote 0

bussi04

Member
Licensed User
Longtime User
This is the project zip and the apk.
 

Attachments

  • hello.zip
    7.2 KB · Views: 192
  • hello.apk
    112.3 KB · Views: 174
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I tested your project.
It works in Debug Mode but crashes in Release Mode.
Trying Clean Project doesn't help.
I made a new project with the same functionality and it works.
I don't know why your project doesn't work.
Try the attached new project.
 

Attachments

  • NewHello.zip
    7.3 KB · Views: 187
Upvote 0

bussi04

Member
Licensed User
Longtime User
Ok, that helped a lot! Now the app is working :)
Thank you Klaus for your support.

Log file says
"sending message to waiting queue (CallSubDelayed - UpdateStatus)
Ignoring event (too many queued events: CallSubDelayed - UpdateStatus)"

What I´m missing is the log text string from the log statements.

Is that normal?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I don't know why your project doesn't work.


B4X:
Timer1.Initialize("Timer1_Tick",5000)

he named the event wrong, if this is the name then the event should look like this:

B4X:
Sub Timer1_Tick_Tick
'...
End sub

you should remove "_Tick" when you intialize the timer

like this:
B4X:
Timer1.Initialize("Timer1",5000)

btw, why is the timer intialized only when firsttime is true?
 
Upvote 0

bussi04

Member
Licensed User
Longtime User
@ilan

you got it right!
and it´s logical too because _Tick is the event name for Timer1 similar to VB.net.

I was mislead by several code snippets all replicating the same error like

https://www.b4x.com/android/forum/threads/timers-loopers-and-callsubplus.65989/#content

and also the book "B4A: Rapid Android App Developmentusing BASIC" by Wyken Seagrave.

Maybe there is a slight B4A compiler flaw nevertheless because started from scratch it also works with "Timer1_Tick" until I add some more code and some librarys and then for "Timer1_Tick" the program stop reaccurs.

Probem solved! Thanks community :)
 
Upvote 0
Top