'Service module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim hc As HttpClient
Dim hcInit As Boolean
hcInit = False
Dim RegisterTask, UnregisterTask As Int
RegisterTask = 1
UnregisterTask = 2
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
ToastMessageShow("Service started", False)
Log("servicestarted")
If StartingIntent.HasExtra("MyRequest") Then
Select StartingIntent.GetExtra("MyRequest")
Case "Register"
RegisterDevice(False)
Case "Unregister"
RegisterDevice(True)
End Select
Else
Select StartingIntent.Action
Case "com.google.android.c2dm.intent.REGISTRATION"
HandleRegistrationResult(StartingIntent)
Case "com.google.android.c2dm.intent.RECEIVE"
MessageArrived(StartingIntent)
End Select
End If
End Sub
Sub Service_Destroy
End Sub
Sub MessageArrived (Intent As Intent)
Dim From, CollapseKey, Data As String
If Intent.HasExtra("from") Then From = Intent.GetExtra("from")
If Intent.HasExtra("data") Then Data = Intent.GetExtra("data")
If Intent.HasExtra("collapse_key") Then CollapseKey = Intent.GetExtra("collapse_key")
'Here you should handle the new message:
Log("New message arrived: " & Data)
ToastMessageShow("New message: " & Data, True)
'Msgbox(Data, "New Message")
Dim n As Notification
n.Initialize
'n.AutoCancel = True
n.SetInfo("New Message", Data, "")
'n.Vibrate = False
n.Notify(1)
End Sub
Sub RegisterDevice (Unregister As Boolean)
Dim i As Intent
If Unregister Then
i.Initialize("com.google.android.c2dm.intent.UNREGISTER", "")
Else
i.Initialize("com.google.android.c2dm.intent.REGISTER", "")
i.PutExtra("sender", Main.SenderId)
End If
Dim r As Reflector
Dim i2 As Intent
i2 = r.CreateObject("android.content.Intent")
Dim pi As Object
pi = r.RunStaticMethod("android.app.PendingIntent", "getBroadcast", _
Array As Object(r.GetContext, 0, i2, 0), _
Array As String("android.content.Context", "java.lang.int", "android.content.Intent", "java.lang.int"))
i.PutExtra("app", pi)
StartService(i)
End Sub
Sub HandleRegistrationResult(Intent As Intent)
If Intent.HasExtra("error") Then
Log("Error: " & Intent.GetExtra("error"))
ToastMessageShow("Error: " & Intent.GetExtra("error"), True)
Else If Intent.HasExtra("unregistered") Then
If hcInit = False Then hc.Initialize("hc")
Dim req As HttpRequest
req.InitializeGet(Main.BoardUrl & "?device_password=" & Main.DeviceBoardPassword & _
"&name=" & Main.DeviceName & "&id=") 'Empty id is sent here. This will cause the board to delete this name.
hc.Execute(req, UnregisterTask)
Else If Intent.HasExtra("registration_id") Then
If hcInit = False Then hc.Initialize("hc")
Dim rid As String
rid = Intent.GetExtra("registration_id")
Dim req As HttpRequest
req.InitializeGet(Main.BoardUrl & "?device_password=" & Main.DeviceBoardPassword & _
"&name=" & Main.DeviceName & "&id=" & rid)
hc.Execute(req, RegisterTask)
End If
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Select TaskId
Case RegisterTask
ToastMessageShow("Registration completed successfully.", False)
Case UnregisterTask
ToastMessageShow("Unregistration completed successfully.", False)
End Select
Response.Release
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Dim errorMsg As String
errorMsg = "Code=" & StatusCode & ", " & Reason
If Response <> Null Then
errorMsg = errorMsg & ", " & Response.GetString("UTF8")
Response.Release
End If
ToastMessageShow(errorMsg, True)
Log(errorMsg)
End Sub