Android Question Service not executing CallSub on Create

Alonski

New Member
Licensed User
Longtime User
Hello I have an app which uses a Service to Catch SMS messages.

To recreate the problem:
1. Run the App.
2. Send an SMS from a different phone to your phone.
3. Read the logs.

What happens now is on the first SMS received the service is Created. CallSub isn't called.
On the second SMS received the service has already been created. CallSub is called.

In my Main:
B4X:
Public Sub Set_Message(msg As Message) As String
    Log("Trying to set Message")
    Log("Message Set")
    Return "SET"
End Sub

In my Service:
B4X:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
        Dim messages() As Message
        messages = ParseSmsIntent(StartingIntent)
        Dim test As String
        test = TryToSetMessage(messages)
        Log(test)
   End If
End Sub

Sub Service_Destroy
   
End Sub

Sub TryToSetMessage(messages() As Message) As String
    Dim test As String
    For i = 0 To messages.Length - 1
        If(messages(i).Address == "000-000-000") Then
             Log("This is the correct Number")
            test = CallSub2(Main, "Set_Message", messages(i))
            Log(test)
            Log("Tried to send message!")
        Else
            Log("Incorrect number")
            test = CallSub2(Main, "Set_Message", messages(i))
            Log(test)
            Log("Failed to send message!")
        End If
    Next
    If test = "SET" Then
        Return "Sub Called Successfully"
    Else
        Return "Sub Not Called Successfully"
    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

I have attached a demo project to show this off.
Any ideas?
 

Attachments

  • mnc.zip
    8 KB · Views: 101

HotShoe

Well-Known Member
Licensed User
Longtime User
In Sub TryToSetMessage(messages() As Message)

Change:

If(messages(i).Address == "000-000-000") Then

to:

If(messages(i).Address = "000-000-000") Then

b4a doesn't understand C syntax well, so the == is likely causing problems.
 
Upvote 0

Alonski

New Member
Licensed User
Longtime User
Thanks I fixed it however that wasn't the problem. It gets into the If statement -> Logs that it has gotten inside the If -> I tell it to CallSub. Doesn't work -> A Log after the CallSub gets called.
 
Upvote 0
Top