Android Question SmsInterceptor Working Example

mpathma

Member
Licensed User
Longtime User
Hi,

I am struggling to get the SmsInterceptor working, I have been searching for few hours now and could not find working example.

I noted some example posted by other but I could not get it working.

My objective is to intercept define format of incoming SMS (only when my apps is running), if during my apps active, the message match my format, I need to stop it from going to InBox.

My Apps will process it.

Can anyone point in the right direction. (working example will be good start.... :p )

Thanks In Advance.:)

M.Pathma
 

gz7tnn

Member
Licensed User
Longtime User
The link that M.Pathma referrs to (#3 above) is the sample code that I provided. I still have not been able to get my version to work the way I would expect so hope that this new thread may give me the correct way to do it.

My app does work, but if the service is NOT running, and the first sms message IS the trigger word then...
--The service starts - and does respond - BUT still lets the message go thru to the default InBox
---All subsequent sms messages with the trigger word are silently handled.
----How do i silently handle the first message if it is the trigger word?

For example, on my phone I have an app called WheresMyDroid that handles sms messages the way I want my app to do. That app 'reads' the incoming text and if it is the code word, will text me the phones location -- AND stops the sms message from going to the default handler.
The thing to note here is that this app and/or service is NOT running all the time, but I see that the service starts and stops when the sms text is received. It is this same thing that I am hoping to achieve

See below for code.
Thanks
 
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
Code for the above question...

I have the following in the activity code
...........................................................................
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("main")
StartService(smsmodule)
End Sub


Below is the code from my service -- smsmodule --.
(Please excuse the many LOG and TOAST entries)
............................................................
Type=Service
Version=2.71
@EndOfDesignText@
#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
Dim SI As SmsInterceptor
SI.Initialize2("SI", 999)
ToastMessageShow("went into service create",True)
Log("#0")
ToastMessageShow("#0",True)
End Sub

Sub Service_Start(startingIntent As Intent)
Log("#1")
ToastMessageShow("#1",True)
If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
Log("#2")
ToastMessageShow("#2",True)
Dim messages() As Message
messages = ParseSmsIntent(startingIntent)
For i = 0 To messages.Length - 1
Log(messages(i))
Next
ToastMessageShow("went into startingIntent.Action",True)

End If

End Sub


'Parses an SMS intent and returns an array of messages
Sub ParseSmsIntent (In As Intent) As Message()
Log("#3")
ToastMessageShow("#3",True)
ToastMessageShow("went into parseSMSIntent.Action",True)
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
Log("#4")
ToastMessageShow("#4",True)
Dim messages(pdus.Length) As Message
Dim From As String
Dim smsin As String
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")

ToastMessageShow(r.RunMethod("getMessageBody"),True)

smsin = r.RunMethod("getMessageBody")
From = r.RunMethod("getOriginatingAddress")


If smsin.ToLowerCase = "secret" Then
SI_MessageReceived(From,smsin)
End If
Next
End If

Return messages
End Sub

Sub SI_MessageReceived (From As String, Body As String) As Boolean
Dim smsOut As PhoneSms
Log("#6")
ToastMessageShow("#6",True)
If Body.ToLowerCase = "secret" Then
Log("#7")
ToastMessageShow("#7",True)
ToastMessageShow(Body, True)
smsOut.Send(From,"from SI_MessageReceived")
ToastMessageShow("Auto-reply sent", True)
Return True
Else
Log("#8")
ToastMessageShow("#8",True)
Return False
End If
End Sub

Sub Button2_Click
ExitApplication
End Sub
 
Upvote 0

mpathma

Member
Licensed User
Longtime User
Hi, gz7tnn,

I though you solve this.

As of now, I could not get the code working for all SMS's. I will try to implement what Eral indicated.

Option B is to continuity scan for incoming SMS to match my search string, then delete it what I found what I need.

M.Pathma
 
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
Hi M.Pathma

That is strange as the second version of the code I posted at the url below does work for me. I extensively tested it on my LG phone before posting it.
You say in #7 that 'I could not get the code working for all SMS's' what are you trying to do there as the code I posted only deals with 1 match.
I presume you understand that the only phrase it will repond to in its current state is the word "secret" and all other sms messages are passed thru to the default InBox.You would need to modify or expand that range of words to suit your purposes.


http://www.b4x.com/android/forum/th...atching-first-text-message.31188/#post-185957

this is the code segment from the service that is doing the work. I have removed the log and ToastMessageShow segments to make it clearer.

If Body.ToLowerCase = "secret" Then '("secret" is the word that it is looking for)
smsOut.Send(From,"from SI_MessageReceived") '("from SI_MessageReceived" is the text of the message that is sent back)
Return True
 
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
Hi Erel

I may not be explaining myself clearly enough in earlier posts and despite scouring everything I can find around intercepting sms messages I am unable to locate anything that tells me how to get around this last issue I have and the link above (Intent Filters - Intercepting SMS messages in the background) does not give me any clue.

I have the service associated with my app working and it will intercept messages, check for a specified word and if it finds that word it will send a responce. Providing the service was already running it will then stop the incoming message from going to the the default sms handler. So far so good.

The situation I cannot find a clear answer to is this:
If the service has stopped - as it may well do - it will start when the next sms is received.
If the first message is not one I need to respond to, it passes thru to the default sms handler
But...
If the first message is one I am interested in then 2 things happen
1> It handles the response (great)
2> However it still allows that same message to be passed to the defaut sms handler (not so great)

There is nothing I have found on this forum that tells me how to handle the situation surrounding that first message.
Are you able to review my code (link is in #8 above) and maybe test it on a device to see what I mean. Once installed the service needs to be stopped and then the first text received needs to just be the word "secret". If it works the same as it does on my LG phone, it will send a response, but you will also get the message passed thru to your normal InBox.

Or do I have wrong expectations about the handling of the first message.
Sorry for the repeated questioning on this but I cant find the correct way to achieve what I want - trap and silently handle that first message
 
Last edited:
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Hi gz7tnn: I can be sure that what you need is to use the BroadcastReceiver library. For the first message, if you want to abort it (don't let any other app to receive the same message) you will need to call BroadcastReceiver.AbortBroadcast. In contrast, if you don't call it you can handle the message as you want and at the same time can let the same message to be received by the default inbox.
B4X:
Sub BroadcastReceiver_OnReceive (Action As String, BroadcastIntent As Object)
If myintent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
        Dim messages() As Message
        messages = ParseSmsIntent(BroadcastIntent)
        If <YOUR CONDITION TO CHECK THE MESSAGE IS HERE> = True Then
            myBroadcastReceiver.AbortBroadcast ' THIS CALL WILL NOT ALLOW MESSAGE TO BE RECEIVED.
            For i = 1 To messages.Length - 1
                  messages(0).Body = messages(0).Body & messages(i).Body
            Next
        End If
    End If
End Sub

The above code is just a clue, you need to adapt your code to your own need...

I am not sure if using of static intent can let you have chance to abort like AbortBroadcast does or not.

P/S. Please post your code using the code function of editor to let others easily read.
 
Last edited:
Upvote 0

mpathma

Member
Licensed User
Longtime User
Hi, gz7tnn,

Can you used the "File -> Expore As Zip" to export your project.

I will be trying your code section again, yes, I am aware of the keyword filtering.

Thanks
 
Upvote 0

mpathma

Member
Licensed User
Longtime User
Hi, All,

I have attached, gz7tnn project in ZIP. Hope can help others.

Tested working on S3, catch 1st SMS without issue.

p.s - I have changed the keyword to "hello"

Thanks gz7tnn.
 

Attachments

  • SmsInterceptor.zip
    8.4 KB · Views: 568
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
Hi Erel, mpathma and bsnqt
Thank you all for your input and suggestions on this one, I have learned a bit from each one...
While I still cannot quite handle that first sms text the way I wanted to, I will run with what I have. Even looking in the Eclipse forums I see my question asked, but unanswered.
Thanks
 
Upvote 0

fricentric

Member
Licensed User
Longtime User
What should i do to make a http post and get back the response and then send a sms according to the response i get?

Below is the code i have used from HttpUtils2Service from http://www.b4x.com/android/forum/threads/httputils2-web-services-are-now-even-simpler.18992/ example:-
B4X:
Sub ParseSmsIntent (In As Intent) As Message()
    Log("#3")
    ToastMessageShow("#3",True)
    ToastMessageShow("went into parseSMSIntent.Action",True)
    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
        Log("#4")
        ToastMessageShow("#4",True)
        Dim messages(pdus.Length) As Message
        Dim From As String
        Dim smsin As String
        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")   

            ToastMessageShow(r.RunMethod("getMessageBody"),True)

            smsin = r.RunMethod("getMessageBody")
            From = r.RunMethod("getOriginatingAddress")
           
            Dim job4 As HttpJob

    'Send a POST request
  job4.Initialize("Job4", Me)
 
  job4.PostString("http://www.mysite.com/api", "smsin="&smsin&"&From="&From)
 
 
            If smsin.ToLowerCase = "hello" Then
                SI_MessageReceived(From,smsin)
            End If
        Next
    End If
       
    Return messages
   
End Sub

i dont get a response at all. Is there some callback function to use to get the response from httputils2service ?
 
Upvote 0

fricentric

Member
Licensed User
Longtime User
How can i get the message id for the sms recieved?
After i get the http response i want to mark the message as read. How can i do that ?
 
Upvote 0
Top