Please see:
GPS tutorial
Toggle Library
Toggle on/off GPS in any Android version
Set phone clock from GPS
Advantages:
- System date and time are updated periodically.
- Without paying a data service.
- You do not need internet access.
- Works in offline mode (airplane mode) to save energy.
Disadvantages:
- Need hardware GPS.
- Need root.
In this code (attached):
- On/Off GPS
- Shell command line [root]
- Get locale GPS DateTime
- Set system DateTime
- Start At Boot
Tested with [Android SDK 19]:
- Android 6 (Only to get GPS data - don't have root)
- Android 2.3.6 (All functions works - with root)
GPSservice Service Module:
Main Activity Module:
GPS tutorial
Toggle Library
Toggle on/off GPS in any Android version
Set phone clock from GPS
Advantages:
- System date and time are updated periodically.
- Without paying a data service.
- You do not need internet access.
- Works in offline mode (airplane mode) to save energy.
Disadvantages:
- Need hardware GPS.
- Need root.
In this code (attached):
- On/Off GPS
- Shell command line [root]
- Get locale GPS DateTime
- Set system DateTime
- Start At Boot
Tested with [Android SDK 19]:
- Android 6 (Only to get GPS data - don't have root)
- Android 2.3.6 (All functions works - with root)
GPSservice Service Module:
B4X:
#Region Service Attributes
#StartAtBoot: False
#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 gps As GPS
Private timer1 As Timer
Dim NewTime As String
Dim lNewTime As Long
Dim lNewTime2 As Long
Dim setEnable As Boolean = True
Dim setNow As Boolean = False
End Sub
Sub Service_Create
timer1.Initialize("timer1", 30000)
timer1.Enabled = True
gps.Initialize("gps")
gps.Start(0, 0)
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_Destroy
End Sub
Sub GPSenable(enableValue As Boolean)
If enableValue Then
gps.Start(0, 0)
Else
gps.Stop
End If
End Sub
Sub gps_NMEA (TimeStamp As Long, Sentence As String)
'Log($"$DateTime{TimeStamp}"$)
'Log(Sentence)
End Sub
Sub GPS_LocationChanged (Location1 As Location)
lNewTime = Location1.Time
'Log("Location Time: " & lNewTime)
NewTime = lNewTime
NewTime = (NewTime.substring2(0,10))
If setNow And setEnable Then
'If ((setNow = True) And (setEnable = True)) Then
shellCmd("date " & NewTime)
'Log("Set date: " & lNewTime)
lNewTime2 = lNewTime
setNow = False
'ToastMessageShow("Set Date & Time", False)
End If
End Sub
Sub timer1_tick()
setNow = True
End Sub
Sub updateTextMain()
CallSubDelayed3(Main, "updateText", lNewTime, lNewTime2)
End Sub
Sub enableAutoSet(setEnable1 As Boolean)
setEnable = setEnable1
End Sub
Sub shellCmd(Command_in As String) As String
'Dim Result As Int
Dim Command, Runner As String
Dim StdOut, StdErr As StringBuilder
Dim Ph As Phone
Runner = File.Combine(File.DirInternalCache, "runner")
Command = File.Combine(File.DirInternalCache, "command")
File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
File.WriteString(File.DirInternalCache, "command", Command_in & CRLF & "exit") 'Any commands via crlf, and exit at end
StdOut.Initialize
StdErr.Initialize
'Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
Return StdOut.tostring
End Sub
Main Activity Module:
B4X:
#Region Project Attributes
#ApplicationLabel: GPS DataTime
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#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.
Private timer1 As Timer
Dim TGL As Toggle
Dim GPStoggle1 As GpsToggle
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.
Private ToggleButton1 As ToggleButton
Private ToggleButton2 As ToggleButton
Private EditText1 As EditText
Private EditText2 As EditText
Private EditText3 As EditText
Private lastData As Long = 0
Private Label1 As Label
Private Label2 As Label
Private Label3 As Label
Private Label4 As Label
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")
StartService(GPSservice)
timer1.Initialize("timer1", 100)
timer1.Enabled = True
Label1.Text = "Format: yyyy/MM/dd HH:mm:ss"
TGL.Initialize()
GPStoggle1.turnGpsOn
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub timer1_tick()
Dim lastDataCalc As Long
Dim lastDataCalcInt As Int
lastDataCalc = (DateTime.Now - lastData)
lastDataCalcInt = lastDataCalc / 1000
CallSubDelayed(GPSservice, "updateTextMain")
EditText1.Text = DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now)
EditText3.Text = lastDataCalcInt & " seconds"
End Sub
Sub updateText(inData As Long, inData2 As Long)
DateTime.DateFormat = "yyyy/MM/dd"
'DateTime.TimeFormat = "HH:mm:ss" ' default
lastData = inData2
EditText2.Text = DateTime.Date(inData) & " " & DateTime.Time(inData)
End Sub
Sub ToggleButton1_CheckedChange(Checked As Boolean)
CallSubDelayed2(GPSservice, "GPSenable", Checked)
ToggleButton2.Enabled = Checked
If ((Checked And TGL.GPS = False) Or (Checked = False And TGL.GPS)) Then
TGL.ToggleGPS
End If
If Checked Then
GPStoggle1.turnGpsOn
Else
GPStoggle1.turnGpsOff
End If
End Sub
Sub ToggleButton2_CheckedChange(Checked As Boolean)
CallSubDelayed2(GPSservice, "enableAutoSet", Checked)
End Sub