Setting System Time and Timezone

rworner

Member
Licensed User
Longtime User
I am working with an MK 802 Android mini PC. I need to have my app reset the system time and timezone when the device. I have set up the SPSntp to get the time and timezone. But how can I replace those values in the system time?
 

rworner

Member
Licensed User
Longtime User
Only system-apps can do that.

I loaded an app called remote adb and ran it on the android mini and then connected to it. Then I used the command:
adb setprop persist.sys.timezone America/Chicago

and it changed the timezone under settings/date & time to Central time - the timezone did not sync up until I issued a reboot command, but then everything reflected Central time.

I tried the command: P.Shell("setprop",Array As String("persist.sys.timezone","America/Chicago"), Null, Null)
but it did not change the timezone under settings/date & time

Any reason why it would work with an ADB connection, but not using the same command in B4A?
 
Upvote 0

rworner

Member
Licensed User
Longtime User
You should pass two StringBuilders to P.Shell. It will allow you to read stdout and stderr.

I manually set the timezone to Eastern (-5)
Then I run the app which should change the timezone. (If I issue the same command using ADB over a usb connection it changes fine.)

#Region Project Attributes
#ApplicationLabel: Set Timezone
#VersionCode: 1
#VersionName: 1.0
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: 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.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")

Msgbox(GetPropInfo, "Set Timezone")

Activity.Finish

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause(UserClosed As Boolean)

End Sub

Sub GetPropInfo As String
' Libs: Core (v/2.47), Phone (v/2.01)
Dim P As Phone
Dim sb,rwout,rwerr As StringBuilder
Dim i As Int
Dim PropData() As String
Dim mycmd,arg1,arg2, stz As String

ProgressDialogShow("Please wait...")

sb.Initialize
rwout.Initialize
rwerr.Initialize
arg1="persist.sys.timezone"
arg2="America/Chicago"
mycmd="setprop"

P.Shell("getprop", Null, sb, Null)
PropData = Regex.Split(CRLF,sb.ToString)
For i = 0 To PropData.Length - 1
If PropData(i).Contains("[persist.sys.timezone]") Then
Log(i & " " & PropData(i))
Exit
End If
Next

P.Shell(mycmd,Array As String(arg1,arg2),rwout,rwerr)

Log("RWOUT: " & rwout)
Log("RWERR: "& rwerr)
P.Shell("getprop", Null, sb, Null)
PropData = Regex.Split(CRLF,sb.ToString)
For i = 0 To PropData.Length - 1
If PropData(i).Contains("[persist.sys.timezone]") Then
Log(i & " " & PropData(i))
stz = PropData(i)
stz = stz.Replace("[","")
stz = stz.Replace("]","")
stz = stz.SubString(stz.IndexOf(" ")).Trim
Exit
End If
Next
ProgressDialogHide
Return "Time Zone : " & stz & CRLF

'
End Sub
 
Upvote 0

rworner

Member
Licensed User
Longtime User
I manually set the timezone to Eastern (-5)
Then I run the app which should change the timezone. (If I issue the same command using ADB over a usb connection it changes fine.)

#Region Project Attributes
#ApplicationLabel: Set Timezone
#VersionCode: 1
#VersionName: 1.0
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: 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.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")

Msgbox(GetPropInfo, "Set Timezone")

Activity.Finish

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause(UserClosed As Boolean)

End Sub

Sub GetPropInfo As String
' Libs: Core (v/2.47), Phone (v/2.01)
Dim P As Phone
Dim sb,rwout,rwerr As StringBuilder
Dim i As Int
Dim PropData() As String
Dim mycmd,arg1,arg2, stz As String

ProgressDialogShow("Please wait...")

sb.Initialize
rwout.Initialize
rwerr.Initialize
arg1="persist.sys.timezone"
arg2="America/Chicago"
mycmd="setprop"

P.Shell("getprop", Null, sb, Null)
PropData = Regex.Split(CRLF,sb.ToString)
For i = 0 To PropData.Length - 1
If PropData(i).Contains("[persist.sys.timezone]") Then
Log(i & " " & PropData(i))
Exit
End If
Next

P.Shell(mycmd,Array As String(arg1,arg2),rwout,rwerr)

Log("RWOUT: " & rwout)
Log("RWERR: "& rwerr)
P.Shell("getprop", Null, sb, Null)
PropData = Regex.Split(CRLF,sb.ToString)
For i = 0 To PropData.Length - 1
If PropData(i).Contains("[persist.sys.timezone]") Then
Log(i & " " & PropData(i))
stz = PropData(i)
stz = stz.Replace("[","")
stz = stz.Replace("]","")
stz = stz.SubString(stz.IndexOf(" ")).Trim
Exit
End If
Next
ProgressDialogHide
Return "Time Zone : " & stz & CRLF

'
End Sub
Stdout and stderr are both empty strings after it completes.
 
Upvote 0

rworner

Member
Licensed User
Longtime User
This program requires root access. See this link: Running shell commands as SuperUser

The device is an MK 802 IIIS. It comes rooted. If I connect a remote ADB app and use the standard "ADB setprop" command it works fine with no special changes and sets the timezone (I do need to reboot the device for the change to take affect after issuing the ADB command). I will try the instructions at the link and let you know how it turns out. Thanks for your time.
 
Upvote 0

rworner

Member
Licensed User
Longtime User
The device is an MK 802 IIIS. It comes rooted. If I connect a remote ADB app and use the standard "ADB setprop" command it works fine with no special changes and sets the timezone (I do need to reboot the device for the change to take affect after issuing the ADB command). I will try the instructions at the link and let you know how it turns out. Thanks for your time.

The Timezone changed but it is not reflected in settings or displayed time until you reboot. I added a sync and reboot command for it to display properly on the device.

The working code:

Dim Command, Runner As String
Dim StdOut, StdErr As StringBuilder
Dim Result As Int
Dim Ph As Phone
StdOut.Initialize
StdErr.Initialize
Runner = File.Combine(File.DirInternalCache, "runner")
Command = File.Combine(File.DirInternalCache, "command")
File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
'Change America/Chicago to the timezone you want the device set to
File.WriteString(File.DirInternalCache, "command", "setprop persist.sys.timezone America/Chicago" & CRLF & "sync" & CRLF & "reboot" & CRLF &"exit") 'Any commands via crlf, and exit at end
Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)

Thank you again Erel!!!
 
Upvote 0
Top