Android Question how to use Timer open startactivity (timer not working)?

joe.dai

Member
Hi

I have two activity pages, when the A activity is started, the timer_tick will run and the B activity page will open

When B's activity page is opened, A's timer will stop?

Excuse me, how can I make the timer of A still run after the start of B

What should I do

B4X:
'====A activity=======
Sub Process_Globals
    Dim Atimer As Timer 
    Dim wifi As MLwifi
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Atimer.Initialize("Atimer_err",5000)
    Atimer.Enabled=True
End Sub
Sub Atimer_err_tick   
    if wifi.isOnline = False then
        StartActivity("B")     
    End if   
End Sub

'====B activity=======

Sub Process_Globals
    Dim Btimer As Timer     
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Btimer.Initialize("Btimer_err",1000)
    Btimer.Enabled=True
End Sub
Sub Btimer_err_tick   
    if A.wifi.isOnline = Ture then
       Activity.Finish()
    End if   
End Sub

please help ...thanks
 

joe.dai

Member
Thanks Erel :)

I changed my code and started the B_Activity test using the enable service.
It works, but I found that if the application is closed by the user, the TimerService will not stop.
How can I stop the TimerService after the program is closed? ??

Thank you for your suggestion

B4X:
'====A_Activity=======
Sub Activity_Create(FirstTime As Boolean)
    StartService(TimeService)
End Sub

'====TimeService=======
Sub Process_Globals     
    Dim wifi As MLwifi
    Dim count as int = 0     
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 

    count = count + 1
    log(count)
    
    DateTime.DateFormat = "HH:mm:ss dd-MM-yyyy"
    Dim current_date As Long  = DateTime.Now
    Log(DateTime.Date(current_date))
    Dim p As Period
    p.Seconds = 3     
    Dim mm As Long = DateUtils.AddPeriod(current_date, p)
    StartServiceAt("",mm,True)   

        IF wifi.isOnline = False then
            StartActivity("B_Activity")
        End if 

End Sub
Sub Process_Globals     
    Dim TimerClose As Timer   
End Sub

'====B_Activity=======
Sub Activity_Create(FirstTime As Boolean)     
    Activity.LoadLayout("B_Activity")   
    TimerClose.Initialize ("TimerCloseQ",5000)
    TimerClose.Enabled = True
End Sub

Sub TimerCloseQ_Tick     
    If TimeService.wifi.isOnline = True Then
        Activity.Finish()                 
    End If   
End Sub
 
Upvote 0

joe.dai

Member
I found the solution to the problem
Adding the following program to the service is normal

thanks Erel

B4X:
Sub Service_Destroy
    StopService("")   
    CancelScheduledService("") 
End Sub
 
Upvote 0
Top