Android Question Why does the widget update stop working after a few seconds?

amir hosein

Member
Hello every one .
I wrote a widget program that include a setting page and 2 services including widget service & socket service .
Sometimes when I put the widget on the home screen , It didn't read my settings that created by setting form .
Besides the above problem , It must work every 5 seconds and connect to a socket and retrieve data and show them on views in widget , but after several 5 seconds , It doesn't work anymore .
ٌWhere is the possibility of the problem ?

Please help me to solve this problem .

Thanks in advance
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Upgrade to latest version of B4A.
2. Use a receiver instead of a service.

 
Upvote 0

udg

Expert
Licensed User
Longtime User
Another problem you may experience with your home widget is after a boot/reboot.
Read here in case you need it.
I consider it a temporary (and complex) solution but it so far worked for me.
 
Upvote 0

amir hosein

Member
Hello again ,
I Downloaded the HelloWidget app. and installed it on my cellphone . No fail encountered and every things went OK.
I added a timer to HelloWidget and in that's event called rv_RequestUpdate and it worked again without fail .
I prepared an application like "HelloWidget" and Added a Socket service to it and called "StartService" in the Receiver_Receive and it faced to fail .
Some time I put widget on screen ,it works and some time not works and it's like hanged . (I have a timer and it's not work some time)
Some time that my timer on my widget is working , after several minutes it stops...


my code:
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    imageFiles = Array As String("alarm1.png", "alarm1_cut.png")
    timerInterval=5
    If FirstTime Then
        rv = ConfigureHomeWidget("wid", "rv", 31, "TankMonitor Widget")
    End If
    rv.HandleWidgetEvents(StartingIntent)
    tmr.Initialize("tmr",2000)
    tmr.Enabled=True
    timerToSetAlarmOn= TIMER_DEFAULT_ALARM_ON
    StartService(Socket_Service)
    setDefaults
    readSettings
End Sub

private Sub rv_Disabled
    StopService(Socket_Service)
    
End Sub

Private Sub rv_RequestUpdate
    SetTime
    rv.UpdateWidget
End Sub

Sub tmr_Tick
    If timerToSetAlarmOn>0 Then  timerToSetAlarmOn = timerToSetAlarmOn - 1
    tmr.Enabled= False
    tmr.Interval= timerInterval * 1000
    tmr.Enabled= True
    updateViews
    rv_RequestUpdate
    
End Sub

Sub updateViews
    If IsPaused(Socket_Service) Then
        StartService(Socket_Service)
        tmr.Enabled= False
        tmr.Interval= 1000
        tmr.Enabled= True
        rv.SetText("lblStatus","Service Starting...")
        rv.UpdateWidget
    End If
    
    If Socket_Service.running Then
        If Socket_Service.connectedIP<>"" Then
            rv.SetText("lblStatus",Socket_Service.connectedIP)
        End If
    End If
End Sub

Private Sub SetTime
    rv.SetText("lblUpTime", DateTime.Time(DateTime.Now))
End Sub

private Sub setDefaults
    Socket_Service.myTanks(0).Capacity =20000
    Socket_Service.myTanks(1).Capacity =20000
    Socket_Service.myTanks(2).Capacity =20000
    Socket_Service.myTanks(3).Capacity =20000
    Socket_Service.myTanks(4).Capacity =20000
    Socket_Service.myTanks(5).Capacity =20000
    Socket_Service.TankNumbers= 2
    Socket_Service.directConnect = True
    IP = "192.168.1.34"
End Sub
 
Upvote 0

amir hosein

Member
I want to have a socket service that call every 5 or 10 or 20 seconds to make a TCP connection with a Device and send a command to it and collect returned data from that device . After getting data from device , the TCP connection can be close .
 
Upvote 0

amir hosein

Member
Hello .
I can't understand exactly what i must do . Can you explain me more ?
May I create a notification object and run that as foreground service ?
 
Upvote 0

amir hosein

Member
Hello again .
I Checked the "HelloWidget" again and I saw that it will not work after about 2 hours .
Why this sample don't work correctly ?
 
Upvote 0

amir hosein

Member
I changed my socket service start subroutine as follows , and my widget works about 25 minutes and then nothing .

Socket Service:
Sub Service_Start (StartingIntent As Intent)
    Service.AutomaticForegroundMode= Service.AUTOMATIC_FOREGROUND_ALWAYS
    running= True
End Sub

Sub Service_Destroy
    If socket1.IsInitialized Then
        socket1.Close
    End If
    If timer1.IsInitialized Then
        timer1.Enabled = False
    End If
    Service.StopAutomaticForeground
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I Checked the "HelloWidget" again and I saw that it will not work after about 2 hours .
Why this sample don't work correctly ?
Not enough information here. Make sure to test it in release mode.

I changed my socket service start subroutine as follows , and my widget works about 25 minutes and then nothing .
Start with this example: Background location tracking

Android doesn't like apps that run in the background. In most cases it is better to use push notifications instead.
 
Upvote 0
Top