Android Question B4A doesn't recognize a Time Zone change in a kiosk app.

Uitenhage

Member
Licensed User
Longtime User
I have a B4A kiosk app that collects data using NFC tags and uploads that data to a PC with Bluetooth. For security reasons the user cannot access the phone's settings screens and the phone does not have a SIM card or WiFi enabled. Because the phone battery will sometimes get completely discharged or the clock simply drifts over time, the user may need to set the date, time and time zone. The app is set as Device Owner and can use Device Policy Manager calls to set the clock.

My problem is that after setting the clock and the time zone with the following code the DateTime.Date function doesn't recognize the new time zone but seems to keep the time zone that was in effect when the app started.

Set the system clock and time zone:
Public Sub SetSystemClock(Millis As Long, TimeZoneId As String) As ResumableSub
    Try
        Dim ctxt As JavaObject
        ctxt.InitializeContext

        Dim dp As JavaObject = ctxt.RunMethod("getSystemService", Array("device_policy"))

        Dim admin As JavaObject
        admin.InitializeStatic(Starter.DEVICE_ADMIN_RECEIVER)

        Dim cn As JavaObject
        cn.InitializeNewInstance("android.content.ComponentName", Array(ctxt, admin))

        ' Disable auto time + auto zone
        dp.RunMethod("setGlobalSetting", Array(cn, "auto_time_zone", "1"))
        dp.RunMethod("setGlobalSetting", Array(cn, "auto_time_zone", "0"))

        dp.RunMethod("setGlobalSetting", Array(cn, "auto_time", "1"))
        dp.RunMethod("setGlobalSetting", Array(cn, "auto_time", "0"))

        ' Apply TimeZone → then clock
        dp.RunMethod("setTimeZone", Array(cn, TimeZoneId))
        dp.RunMethod("setTime", Array(cn, Millis))
        
        DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss z"
        LogColor($"SetSystemClock: ${DateTime.Date(DateTime.Now)}"$, Colors.Cyan)

    Catch
        LogColor($"SetSystemClock ERROR: ${LastException}"$, Colors.Red)
    End Try

    Dim sys As JavaObject
    sys.InitializeStatic("java.lang.System")
    Return sys.RunMethod("currentTimeMillis", Null)
End Sub

Retrieve the time zone from the system:
Public Sub ShowTimeZone As String
    Dim TimeZone As JavaObject
    TimeZone.InitializeStatic("java.util.TimeZone")
    Dim currentTimeZone As JavaObject = TimeZone.RunMethod("getDefault", Null)
    Dim TimeZoneID As String = currentTimeZone.RunMethod("getID", Null)
    LogColor($"ShowTimeZone TimeZone ID: ${TimeZoneID}"$, Colors.Cyan)
    Return TimeZoneID
End Sub

The attached app demonstrates this. On the main page the "Set the system clock" button opens a screen to set the date, time and time zone; the "Go to the Date Settings button" opens the system date/time settings screen (at least it does on a Samsung phone) so you can verify that the change has been made. The second screen has some Wheel Pickers to set the date and time and a Spinner to select a time zone. The "Set The System Clock" button runs the "SetSystemClock" above. After setting clock with these values return to the main screen and note that the B4A date still has the old time zone but the system reports the one you selected.

What am I missing? Have I been looking at this so long I can't see it any more?
 

Attachments

  • KioskSetDate.zip
    17.6 KB · Views: 9

Uitenhage

Member
Licensed User
Longtime User
Are you calling DateTime.ListenToExternalTimeChanges ?
Thank you! I was not calling it. I added it to my Starter service and everything looks great. I also added the DateTime_TimeChanged but if I change the Time Zone several times the change is reflected correctly in the app but the log lines only appear in the log after the first change.

DateTime.ListenToExternalTimeChanges event code:
Public Sub DateTime_TimeChanged
    Dim t As String =    DateTime.DateFormat
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss z"
    LogColor("*****************************", Colors.Red)
    LogColor($"DateTime_TimeChanged: ${DateTime.Date(DateTime.Now)}, ${SetClock.ShowTimeZone}"$, Colors.Yellow)
    LogColor("*****************************", Colors.Red)
    DateTime.DateFormat = t
End Sub

This is more a side comment than a question.
 
Upvote 0
Top