Android Question SMS <startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED"> don't work in B4XPages

Diego Marcelo Croatto

Member
Licensed User
Hi to all.... I'm trying to catch a SMS incomming message in a B4XPages project but the "startingIntent.Action" event never happen.
But!.... if I make a default project with the same code... the same manifest permissions.... it work find!... What am I doing wrong?

Thank's for the help or example code....

manifest:
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(s1,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)

s1 Code:
#Region  Service Attributes
    #StartAtBoot: False
    
#End Region

'Service module
Sub Process_Globals
    Type Message (Address As String, Body As String)
    
End Sub

Sub Service_Create

End Sub

Sub Service_Start(startingIntent As Intent)
    If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then   '<----- In B4XPages project never run!... but in Default project yes!
        Dim messages() As Message
        messages = ParseSmsIntent(startingIntent)
        For i = 0 To messages.Length - 1
            Log(messages(i))
        Next
    End If
    'Service.StopAutomaticForeground
End Sub

Sub Service_Destroy
    Log("goodbye")
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")
            If messages(i).Address = Main.blkNum Then
                'Log("Carrie")
                SendMessage
            End If
        Next
    End If
    
    Return messages
End Sub


Sub SendMessage
    Log("Don Got Here")
    Dim PhoneNumber As String = Main.blkNum
    Dim SMS As PhoneSms
    Dim Message As String = "The SMS Message to recipient Above Could not be Delivered.. Please Try again later.. "
    SMS.Send(PhoneNumber,Message)
End Sub

B4XMainPage Initialize sub:
StartService(s1)
 
Top