First, because in services can't start timer, so I use sleep to loop it.
And then..
When I called "Job2.Download"
JobDone that sub didn't do anything- -!
When this code in activity, It's work..
This app have main(activity), s1(services), s2(services), HttpUtilsServices(services).
main(the code I call s2):
s2 :
And then..
When I called "Job2.Download"
JobDone that sub didn't do anything- -!
When this code in activity, It's work..
This app have main(activity), s1(services), s2(services), HttpUtilsServices(services).
main(the code I call s2):
B4X:
StopService(s1)
StartService(s2)
s2.username = EditText1.Text
s2.password = EditText2.Text
s2.pc = Spinner1.SelectedItem
s2.phonenum = EditText5.Text
s2 :
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
'Service module
Sub Process_Globals
Dim username As String
Dim password As String
Dim pc As String
Dim phonenum As String
Dim smsc As String
Dim phc As String
Dim chb As String
Dim TimerN As Int
Dim Job1, Job2 As HttpJob
Dim s As String
Dim t As Int
Dim SMS1 As PhoneSms
Dim Timer2 As Timer
Timer2.Initialize("Timer2",2500)
'Timer2.Enabled=True
Dim n As Notification
n.Initialize
n.Icon = "icon"
End Sub
Sub Service_Create
DateTime.DateFormat="hh:mm:ss"
n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", username & "_" & password & "_" & pc & "_" & phonenum, Main) 'Change Main (above) to "" if this code is in the main module.
n.Notify(1)
Timer2.Interval = 1500
'Timer2.Enabled = True
TimerN = 0
t = 0
Do Until (t=1)
Sleep(1000)
Sleep(1000)
Sleep(1000)
Sleep(1000)
If TimerN = 0 Then
TimerN = 1
DateTime.DateFormat="hh:mm:ss"
n.SetInfo("1.[" & DateTime.Date(DateTime.now) & "]", "Checking sms code from bot.", Main) 'Change Main (above) to "" if this code is in the main module.
n.Notify(1)
chb = "http://xxx/auth.php?username=" & username & "&password=" & password & "&pc=" & pc
Dim Job2 As HttpJob
Job2.Initialize("Job2", Me)
Job2.Download(chb)
Job2.GetRequest.Timeout = 2000
Else If TimerN = 1 Then
TimerN = 0
DateTime.DateFormat="hh:mm:ss"
n.SetInfo("2.[" & DateTime.Date(DateTime.now) & "]", "Checking sms code from bot.", Main) 'Change Main (above) to "" if this code is in the main module.
n.Notify(1)
chb = "http://xxx/auth.php?username=" & username & "&password=" & password & "&pc=" & pc
Dim Job2 As HttpJob
Job2.Initialize("Job2", Me)
Job2.Download(chb)
Job2.GetRequest.Timeout = 1500
End If
Loop
End Sub
Sub Sleep(ms As Long)
Dim now As Long
If ms > 1000 Then ms =1000 'avoid application not responding error
now=DateTime.now
Do Until (DateTime.now>now+ms)
DoEvents
Loop
End Sub
'
'Sub Timer2_Tick
' n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", "Checking sms code from bot.", Main) 'Change Main (above) to "" if this code is in the main module.
' n.Notify(1)
' Return
' 'Handle tick events
' If TimerN = 0 Then
' TimerN = 1
' DateTime.DateFormat="hh:mm:ss"
' n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", "Checking sms code from bot.", Main) 'Change Main (above) to "" if this code is in the main module.
' n.Notify(1)
'
' 'Label6.Text = "2.[" & DateTime.Date(DateTime.now) & "]" & "Checking sms code from bot."
' chb = "http://xxx/auth.php?username=" & username & "&password=" & password & "&pc=" & pc
'' Job2.CallbackActivity = "Main" 'Current activity name.
'' Job2.CallbackJobDoneSub = "JobDone2"
'' Job2.Download("Job2", chb)
'' Job2.GetRequest.Timeout = 1000
' Dim Job2 As HttpJob
' Job2.Initialize("Job2", Me)
' Job2.Download(chb)
' Job2.GetRequest.Timeout = 2000
' Else If TimerN = 1 Then
' TimerN = 0
' DateTime.DateFormat="hh:mm:ss"
' n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", "Checking sms code from bot.", Main) 'Change Main (above) to "" if this code is in the main module.
' n.Notify(1)
'
' 'Label6.Text = "1.[" & DateTime.Date(DateTime.now) & "]" & "Checking sms code from bot."
' chb = "http://xxx/auth.php?username=" & username & "&password=" & password & "&pc=" & pc
'' Job2.CallbackActivity = "Main" 'Current activity name.
'' Job2.CallbackJobDoneSub = "JobDone2"
'' Job2.Download("Job2", chb)
'' Job2.GetRequest.Timeout = 1000
' Dim Job2 As HttpJob
' Job2.Initialize("Job2", Me)
' Job2.Download(chb)
' Job2.GetRequest.Timeout = 1500
' Else If TimerN = 2 Then
' TimerN = 3
' DateTime.DateFormat="hh:mm:ss"
' n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", "Updating sms code to bot.", Main) 'Change Main (above) to "" if this code is in the main module.
' n.Notify(1)
' 'Label6.Text = "4. [" & DateTime.Date(DateTime.now) & "]" & "Updating sms code to bot."
' chb = "http://xxx/auth.php?username=" & username & "&password=" & password & "&pc=" & pc & "&sms=" & smsc
' Dim Job2 As HttpJob
' Job2.Initialize("Job1", Me)
' Job2.Download(chb)
' Job2.GetRequest.Timeout = 2000
' Else If TimerN = 3 Then
' TimerN = 2
' DateTime.DateFormat="hh:mm:ss"
' n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", "Updating sms code to bot.", Main) 'Change Main (above) to "" if this code is in the main module.
' n.Notify(1)
' 'Label6.Text = "3. [" & DateTime.Date(DateTime.now) & "]" & "Updating sms code to bot."
' chb = "http://xxx/auth.php?username=" & username & "&password=" & password & "&pc=" & pc & "&sms=" & smsc
' Dim Job2 As HttpJob
' Job2.Initialize("Job1", Me)
' Job2.Download(chb)
' Job2.GetRequest.Timeout = 1500
' End If
'End Sub
Sub JobDone (Job As HttpJob)
n.SetInfo("[" & DateTime.Date(DateTime.now) & "]","JobDone", Main) 'Change Main (above) to "" if this code is in the main module.
n.Notify(2)
'Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
If Job.Success = True Then
Select Job.JobName
Case "Job1"
'print the result to the logs
s = Job.GetString
n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", s, Main) 'Change Main (above) to "" if this code is in the main module.
n.Notify(1)
Msgbox(s,"完成")
Timer2.Enabled = False
Case "Job2"
'show the downloaded image
s = Job.GetString
n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", Job.GetString, Main) 'Change Main (above) to "" if this code is in the main module.
n.Notify(2)
'Msgbox(s,"test")
If s.Length > 11 Then
phc = s.SubString(s.Length - 12 + 1)
Timer2.Enabled = False
If t < 1 Then
SMS1.Send(phonenum,s.SubString(s.Length - 12 + 1))
DateTime.DateFormat="hh:mm:ss"
n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", "Done!! Sended " & s.SubString(s.Length - 12 + 1) & " to Apple.", Main) 'Change Main (above) to "" if this code is in the main module.
n.Notify(1)
'Label6.Text = "[" & DateTime.Date(DateTime.now) & "]Done!! Sended " & s.SubString(s.Length - 12 + 1) & " to Apple."
t = 1
End If
End If
End Select
Else
'Log("Error: " & Job.ErrorMessage)
'ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub
Sub Service_Start(startingIntent As Intent)
If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
Dim messages() As Message
messages = ParseSmsIntent(startingIntent)
For i = 0 To messages.Length - 1
Log(messages(i))
If Main.sf.InString(messages(i).Body.SubString(messages(i).Body.Length - 9 + 1)," ") = -1 Then
If messages(i).Address.SubString(messages(i).Address.Length - 9 + 1) = phonenum Then
smsc = messages(i).Body.SubString(messages(i).Body.Length - 9 + 1)
TimerN = 2
Timer2.Interval = 1500
Timer2.Enabled = True
End If
End If
'End If
Next
'Return TestStr
'CallSubDelayed(Main,"show_msg")
End If
End Sub
'Parses an SMS intent and returns an array of messages
Sub ParseSmsIntent (In As Intent) As Message()
Dim messages() As Message
If In.HasExtra("pdus") = False Then Return messages
Dim pdus() As Object
Dim r As Reflector
pdus = In.GetExtra("pdus")
If pdus.Length > 0 Then
Dim messages(pdus.Length) As Message
For i = 0 To pdus.Length - 1
r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
Array As Object(pdus(i)), Array As String("[B"))
messages(i).Body = r.RunMethod("getMessageBody")
messages(i).Address = r.RunMethod("getOriginatingAddress")
Next
End If
Return messages
End Sub