Android Tutorial Update a homescreen widget more frequently than 30 minutes

Normally a homescreen widget can only update itself every 30 minutes. A lower value is not possible in the ConfigureHomeWidget() call.

But what if you want to create something like a clock which needs to be updated every minute?

This is possible too but I experienced some problems handling the widgetservice correctly in this case. If it is not handled correctly the service will run forever even if no widget is visible on the homescreen anymore. I will explain it in this tutorial on a simple clock widget example.

B4X:
Sub Service_Create
   rv = ConfigureHomeWidget("l2x1", "rv", 120, "AmberHome Minimalistic Clock")
End Sub

We configure our widget in Service_Create(). The update interval can be set to 0 here. It really shouldn't matter because we update the widget ourself. I used a value of 120 because I wanted to be sure that the clock will restart at least after 2 hours when an unexpected problem occurs.

B4X:
Sub Service_Start (StartingIntent As Intent)
   ' If a widget is removed from homescreen we don't need to do anything
   If StartingIntent.Action = "android.appwidget.action.APPWIDGET_DELETED" Then Return
   
   ' Handle the widget events
   If rv.HandleWidgetEvents(StartingIntent) = False Then
      ' If the action is not handled by HandleWidgetEvents() then we
      ' probably were called by StartService() or StartServiceAt().
      ' So just update the widget.
      rv_RequestUpdate
   End If
   
   ' We have to be sure that we do not start the service
   ' again if all widgets are removed from homescreen
   If StartingIntent.Action <> "android.appwidget.action.APPWIDGET_DISABLED" Then
      StartServiceAt("", TimeToNextMinute, False)
   End If
End Sub

To handle the service correctly we have to react correctly on the Intent actions. The commets should explain everything here enough.

If a widget (not the last one) is removed from the homescreen nothing has to be done so we just return.

If the Events are handled by HandleWidgetEvents() then we don't need to do anything special. Otherwise we just update the widget.

At last we have to reschedule our service at the next full minute. Attention: We have to be sure that we don't reschedule our service when the last widget is removed from homescreen. Otherwise our service won't stop correctly!

B4X:
Sub rv_Disabled
   CancelScheduledService("")
   StopService("")
End Sub

If the last widget is removed we have to cancel the last schedule and completely stop the service.

The other subs in the program are trivial. Look at the example code.

PS: I'm always amazed how simple and fast it is to create apps with B4A!
 

Attachments

  • AHClock.zip
    5.7 KB · Views: 1,767
Last edited:

CyclopDroid

Well-Known Member
Licensed User
Longtime User
The problems were 2.
1) I added CancelScheduledService ("") and StopService ("") inside the Service_Destroy (over that in rv_Disable (RemoteView);
2) The issue of the closure was an app cleaner (ZDbox) who cleaned programs from memory (shutting the app) and close it.
 
Top