Android Question HttpUtils2 in Services not work

AndrewChan0513

Member
Licensed User
Longtime User
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):
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
 

AndrewChan0513

Member
Licensed User
Longtime User
Here is my full source code.

Because I'v tried 2 days.
If in the service start timer, the app will crash, and say XXX is stoped.
And if I don't start timer,
Everything is ok - -!
So I deduce services is not support timer.
 

Attachments

  • chbv8.zip
    455.8 KB · Views: 171
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
Okay. - -!
Before I've tried use log - -
but I can't success every time.
So I don't really want to use log....
I try it tonight...
 

Attachments

  • chbv8.zip
    30.4 KB · Views: 135
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
Here is the error after I start timer

21:32:36.271 I/B4A (11427): ~i:** Activity (main) Create, isFirst = true **
21:32:36.371 I/B4A (11427): ~i:** Activity (main) Resume **
21:32:36.491 I/B4A (11427): ~i:** Service (s1) Create **
21:32:36.491 I/B4A (11427): ~i:** Service (s1) Start **
21:32:36.501 I/B4A (11427): ~i:** Service (pushservice) Create **
21:32:36.501 I/B4A (11427): ~i:** Service (pushservice) Start **
21:32:37.212 I/B4A (11427): ~i:** Service (pushservice) Start **
21:32:37.232 I/B4A (11427): ~i:** Service (httputils2service) Create **
21:32:37.242 I/B4A (11427): ~i:** Service (httputils2service) Start **
21:32:42.127 I/B4A (11427): ~i:** Service (s1) Destroy **
21:32:42.137 I/B4A (11427): ~i:** Service (s2) Create **
21:32:42.157 I/B4A (11427): ~i:** Service (s2) Start **
21:32:43.688 I/B4A (11427): ~e:java.lang.NullPointerException
21:32:43.688 I/B4A (11427): ~e: at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:104)
21:32:43.688 I/B4A (11427): ~e: at android.os.Handler.handleCallback(Handler.java:725)
21:32:43.688 I/B4A (11427): ~e: at android.os.Handler.dispatchMessage(Handler.java:92)
21:32:43.688 I/B4A (11427): ~e: at android.os.Looper.loop(Looper.java:137)
21:32:43.688 I/B4A (11427): ~e: at android.app.ActivityThread.main(ActivityThread.java:5293)
21:32:43.688 I/B4A (11427): ~e: at java.lang.reflect.Method.invokeNative(Native Method)
21:32:43.688 I/B4A (11427): ~e: at java.lang.reflect.Method.invoke(Method.java:511)
21:32:43.688 I/B4A (11427): ~e: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
21:32:43.688 I/B4A (11427): ~e: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
21:32:43.688 I/B4A (11427): ~e: at dalvik.system.NativeStart.main(Native Method)
 

Attachments

  • chbv8.zip
    30.2 KB · Views: 149
Last edited:
Upvote 0
Top