Android Question Question about the widget

  • Thread starter Thread starter Deleted member 103
  • Start date Start date
  • Similar Threads Similar Threads Similar Threads
D

Deleted member 103

Guest
Hi,

I use this code for my widget.
It actually works fine, just sometimes the numbers for the time disappear, and I need to restart the widget.
Where could the error be?

Code:
#Region  Service Attributes
    #StartAtBoot: 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.

    Private rv As RemoteViews
    Public textsize As Int
End Sub

Sub Service_Create
   
    rv = ConfigureHomeWidget("frmWidget", "rv", 0, "HomeClock", True)
    rv.SetTextSize("lblTime", textsize)
   
    DateTime.TimeFormat="HH:mm"
    DateTime.DateFormat="EEEE, dd MMM"
End Sub

Sub Service_Start (StartingIntent As Intent)
    If rv.HandleWidgetEvents(StartingIntent) Then Return
       
    'Reschedule the service every minute, so the clock gets updated correctly
    StartServiceAt(Me, TimeToNextMinute, False)
    rv_RequestUpdate
End Sub

Sub Service_Destroy

End Sub

'Calculate the time to the next full minute
Sub TimeToNextMinute As Long
    Dim ret As Long

    ret = DateTime.Now + DateTime.TicksPerMinute
    ret = ret - (ret Mod DateTime.TicksPerMinute)   
    Return ret
End Sub

Sub SetTime
    rv.SetText("lblTime", DateTime.Time(DateTime.Now))
    rv.SetText("lblDate", DateTime.Date(DateTime.Now))
End Sub

Sub rv_RequestUpdate
    SetTime
    rv.UpdateWidget
End Sub

Sub rv_Disabled
    StopService("")
End Sub

homeclock.png
 
BTW, TimeToNextMinute is not really required. It will not be accurate anyway.
How would you do it? I have already tried with a timer, but the time is very often not updated.

Normally works always, only times when I e.g. a photo make then the time is no longer visible.
 
Upvote 0
Upvote 0
Hi Erel,

Unfortunately it does not work well.
homeclock_01.png

Code:
#Region  Service Attributes
    #StartAtBoot: 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.

    Private rv As RemoteViews
    Public textsize As Int
End Sub

Sub Service_Create
   
    rv = ConfigureHomeWidget("frmWidget", "rv", 0, "HomeClock", True)
    rv.SetTextSize("lblTime", textsize)
   
    DateTime.TimeFormat="HH:mm"
    DateTime.DateFormat="EEEE, dd MMM"
End Sub

Sub Service_Start (StartingIntent As Intent)
    'Log(StartingIntent)
           
    'Reschedule the service every minute, so the clock gets updated correctly
    'StartServiceAt(Me, TimeToNextMinute, False)
   
    StartServiceAtExact2(Me, TimeToNextMinute)
   
    rv_RequestUpdate

    If rv.HandleWidgetEvents(StartingIntent) Then Return

End Sub

Sub Service_Destroy

End Sub

Sub StartServiceAtExact2 (ServiceName As String, Time As Long)
    Log("ServiceName=" & ServiceName)
    Dim p As Phone
    If p.SdkVersion < 23 Then
        StartServiceAtExact(ServiceName, Time, True)
    Else
        Dim in As Intent, ctxt As JavaObject, pi As JavaObject
        in.Initialize("", "")
        in.SetComponent(Application.PackageName & "/." &  ServiceName.ToLowerCase)
        ctxt.InitializeContext
        Dim am As JavaObject = ctxt.RunMethod("getSystemService", Array("alarm"))
        pi = pi.InitializeStatic("android.app.PendingIntent").RunMethod("getService",  Array(ctxt, 1, in, 134217728))
        am.RunMethod("setExactAndAllowWhileIdle", Array(0, Time, pi))
    End If
End Sub

'Calculate the time to the next full minute
Sub TimeToNextMinute As Long
    Dim ret As Long

    ret = DateTime.Now + DateTime.TicksPerMinute
    ret = ret - (ret Mod DateTime.TicksPerMinute)   
    Return ret
End Sub

Sub SetTime
    rv.SetText("lblTime", DateTime.Time(DateTime.Now))
    rv.SetText("lblDate", DateTime.Date(DateTime.Now))
End Sub

Sub rv_RequestUpdate
    SetTime
    rv.UpdateWidget
End Sub

Sub rv_Disabled
    StopService("")
End Sub
 

Attachments

Upvote 0
OK, as foreground service it work.

But this clock widget of HTC works without foreground service, how does it work?

homeclock.png
 
Upvote 0
Thank you Erel for your patience, but I do not understand it. :(
Moreover TextClock does not exist in B4a, right?

I wanted to watch for myself a quite simply widget clock, without weather or anything else, but like that is not so easy.
 
Upvote 0
Moreover TextClock does not exist in B4a, right?
No. But you can add it to the generated XML layout.

I wanted to watch for myself a quite simply widget clock, without weather or anything else, but like that is not so easy.
That's true. Widgets are updated every 30+ minutes. This is not ideal for a clock.
 
Upvote 0
Just set the updates to 30 minutes or more so you will be able to update the date.
Have already tried, without success. :(

The problem is only from Android 7.0+, with 5.1 there are no problems.

It seems to be running properly here (I've removed the StartServiceAt calls). Are you testing it in release mode?
Yes :(

In this version I try the service every 5 minutes to start, but the test is not yet finished.
So it has been running since this morning, and does not look bad. :)
 
Upvote 0
I've installed it yesterday on an Android 8 device. It still shows the correct time.
Thank you for the info! :)
My installation on Android 7.0 has been working perfectly since yesterday. ;)

I would say "problem solved"!
 
Upvote 0

Similar Threads

Back
Top