Android Question Is the code in thread " Intent Filters - Intercepting SMS messages in the background " still Valid for 2021


New Member
I have duplicated the code from " Intent Filters - Intercepting SMS messages in the background " in b4a version 10.2... Nothing happens when a SMS is received... I added the line " StartActivity(Main)" as suggested by a user to show if the service would bring up the main activity... nothing happens.. If I could add a message box to the 'Service module to show if its even running but this is not allowed.. The line "Log(messages(i))" shows a warning,, but even without that line the service should start MainActivity... Any thoughts would be appreciated.. thanks..

My 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
        Dim messages() As Message
        messages = ParseSmsIntent(startingIntent)
        For i = 0 To messages.Length - 1
            ' Log(messages(i))
    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")
    End If
    Return messages
End Sub


New Member
Thanks For the Reply... I did get it to work fine on samsung a71... I called a sub instead of "StartActivity(Main)" and sent an sms reply right back to incoming sms ,,saying there sms could not be delivered.... Of course its delivered,, but looks to the sender as if it was blocked,, which stops ex-girlfriends from sending unwanted sms... Yes I could just block sms's but the sender gets no reply and keeps sending... Thanks All..
Upvote 0