#Region Service Attributes
#StartAtBoot: True
#StartCommandReturnValue: android.app.Service.START_STICKY
#End Region
Sub Process_Globals
Private MySocket As Socket
Private syn As AsyncStreams
Public Connected As Boolean
Private Timer1 As Timer
End Sub
Sub Service_Create
Service.StartForeground(0,Null)
Timer1.Initialize("tmr",200)
End Sub
Sub Service_Start (StartingIntent As Intent)
Timer1.Enabled = True
End Sub
Sub tmr_Tick
If Connected = False Then
If CheckConnection Then
StartSocket
Timer1.Enabled = False
End If
End If
End Sub
Sub Service_Destroy
Log("destory service")
End Sub
Sub StartSocket
MySocket.Initialize("socket")
MySocket.Connect("0.0.0.0",3003,0)
End Sub
Sub socket_Connected (Successful As Boolean)
If Successful Then
If Connected Then
Return
End If
Dim id As String
id = "device_id=" & rand(1000,9000)
Try
syn.Initialize(MySocket.InputStream,MySocket.OutputStream,"sync")
syn.Write(id.GetBytes("UTF8")) 'register device in system
Connected = True
Log("Connect to server")
Catch
Connected = False
End Try
Else
Timer1.Enabled = True
End If
End Sub
Sub sync_NewData (Buffer() As Byte)
Dim data As String
data = BytesToString(Buffer,0,Buffer.Length,"UTF-8")
If data = "registered device successfully" Then
Return
End If
Try
Dim js As JSONParser
js.Initialize(data)
Catch
Return
End Try
End Sub
Sub sync_Error
MySocket.Close
syn.Close
Connected = False
Log("error stream")
Timer1.Enabled = True
End Sub
Sub sync_Terminated
MySocket.Close
Connected = False
Log("error terminate")
Timer1.Enabled = True
End Sub
Sub sync_NewStream (Dir As String, FileName As String)
End Sub
Sub CheckConnection As Boolean
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
r.Target = r.RunMethod("getActiveNetworkInfo")
If r.Target <> Null Then
Return r.RunMethod("isConnected")
End If
Return False
End Sub