smsInterceptor not catching first text message

gz7tnn

Member
Licensed User
Longtime User
Firstly - thank you for a brilliant product. My use of it is purely for personal pleasure, but have written a few apps already. One of these is a recent go at capturing and auto responding to text messages. I have a working app (full code attached) but it is not quite starting up as I would expect and hope someone can see where I have gone wrong, or have missed something.

What I am trying to do here is intercept each text, and if the body of the message is the word 'secret' then respond to the text and not allow it to go proceed to the default message handler. While it does this I find that:
1> if my app is not running and a text is received, the app / service does start, but does not trap the first text even if it contains the word 'secret'. On that first occasion it bypasses my app and is handled by the default one. The second and subsequent text are however trapped and processed correctly.
2> If I don't have the sNotif segment in my code then the service stops after a while.

While I can live with my way, I am interested to know what the correct approach should be.
 

Attachments

  • SmsReplyForComment.zip
    489.2 KB · Views: 442

gz7tnn

Member
Licensed User
Longtime User
Thanks Erel. I re-read that turorial and coupled it with some other posts. I now have it working. Cheers
 
Upvote 0

mpathma

Member
Licensed User
Longtime User
Hi,
I am struggling to get the SmsInterceptor working, the closest I came to is your application, the one with the original post, you indicated you made it working, can you share the code.

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.

Thanks In Advance.
M.Pathma
 
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
Hi
I have uploaded a slightly better version for you to look at, however it still does not work quite the way I envisaged the service would work. Maybe others can review and comment on it.

In this one:
The service starts when the app is run
It will catch any sms with the defined format, handle the response and also stop it from going to the default InBox .
What I have found with this approach is that after you run the app:
If the Home button on the device is used when the app is active the app and service remain active.
If the Back button on the device is used when the app is active, the app is stopped but service remains active.
If the 'Stop Service and Quit' button on the app (calls ExitApplication) is used then both the app and the service are stopped.
If the app is NOT running when an sms is received, then the service starts but the app does not.
Note here that if the first sms received in this way (ie starts the service) has the 'defined format' then even though the service starts - and does handle the reply correctly- that first message is still passed thru to the default InBox as well.

Reading what you are after, (only when my apps is running) I dont think that will be possible as the service will start and handle the sms automatically.

You and I are after slightly different things and hopefully someone may be able to answer both of these for us.
You want the intercept to only happen when your app is running.
I want (assuming the service is not running) for it to start when the first sms is received, and not pass it through to the default handler if that sms contains the 'defined format'.
 

Attachments

  • smsexample.zip
    18.2 KB · Views: 429
Upvote 0

marcocim

Member
Licensed User
Longtime User
Hi bsnqt,

do you have a version of SmsReply starting at boot ? (without the need to start the application ?)

Thanks
Marco
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
I want (assuming the service is not running) for it to start when the first sms is received, and not pass it through to the default handler if that sms contains the 'defined format'.

I don't know if I understand well your question and need. However it seems to me that what you need is to search and see how to work with Stickly mode of Service (your application in the background but the Service is still working) or search the words "running Service without UI" or so on. There are many of examples available in the forum. Note that a Service can still catch the SMS without the Activity...

Secondly, you don't need to call "ExitApplication". When you call it, both your Activity and Service will be gone and you cannot intercept the SMS anymore. Instead you can use Activity.Finish

I even downloaded your project but I cannot understand your question very well, if possible please reformat your question in a more simple way (clearer) so that others can help you too. And please do not duplicate your question in 2 different posts as it makes difficult to follow the thread...
 
Last edited:
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
Hi

Perhaps the best description of the problem can be made by someone compiling and running the attached code on their mobile device to witness the way it works.
My experience of the Android service that is started, is that it often gets killed. When it does, it is restarted when the next sms message is received.
My problem is how my service handles that first message, but only when the message body contains the specific word it is designed to detect. The way it works at the moment is that - while it does send the response correctly - it still allows that message to pass thru to the default sms handler on the phone.
I dont want that to happen.

Below are the log entries that are generated from my service.
If someone is prepared to compile and load this app onto their phone and then follow the steps below, they should experience what I do.

I have repeated the following test scenario several times with the same result
Compile and install the app on my phone
Stop the service running (via the settings menu)
Send 'secret' to my phone from another phone
###{the other phone receives the correct response BUT my phones default sms handler also handles the message} I want to hide this !!!
review the log entries
Confirm that the service is now active
send the word 'secret' a second time to my phone
review the logs
###{This second text is responded to correctly and on this occasion the sms is trapped and not sent to the default sms handler}



-------------{this is the log for the first text BEFORE the service is running}----------
** Service (smsmodule) Create **
#0 went into Service Create
** Service (smsmodule) Start **
#1 Service Start
#2 went into startingIntent.Action
#3 went into parseSMSIntent.Action
#4
Message Body is: Secret
Calling Number is: +64272038977
going now to SI_MessageReceived
#6 into si_messageReceived
#7 Detected the trigger word to send reply
auto-reply sent
[Address=+64272038977, Body=Secret, IsInitialized=false
]
-------------{this is the log following the text AFTER the service is running}-----------
#6 into si_messageReceived
#7 Detected the trigger word to send reply
auto-reply sent


I have read many posts around sms and services, but my inexperience has not allowed me to find a solution. There have been several suggestions but I have struggled with code snippets or suggestions. If anyone can provide a solution as to how to trap and hide that first text (when it contains the word 'secret'), and repost a the full code would be appreciated.
 

Attachments

  • smsReplyExample.zip
    18.2 KB · Views: 298
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Hi gz7tnn

I have downloaded and tested your app, in both times I can receive correctly the response.
To hide the text message (not allowing the default Inbox to handle the message) I believe you have to use BroadcastReceiver library, please search and read it. You can use "Broadcast.Abort" to prevent other receivers handle your coming messages.

Merry Christmas.
 
Upvote 0

gz7tnn

Member
Licensed User
Longtime User
Hi bsnqt

Thanks for following this with me. I have looked at BroadcastReceiver and have attached an example I have put together using that approach - but it still does not handle the first sms message when the service starts.
Let me explain what I want in a different way. This scenario is entirely fictional as no-one would want this to happen, but it hopefully serves to describe the situation I still have.

Lets say I NEVER want to have any sms messages go into the default app on my phone - I ALWAYS want them to be intercepted and aborted. -- as I said 'entirely fictional'.

The code attached is such an app using BroadcastReceiver and the Broadcast.AbortBroadcast approach as described by yourself.

It works fine when the service is running as it catches and aborts the message from going to the default app.
But if the service is stopped for whatever reason, then it does NOT catch and abort the first sms message - it allows it to pass thru to the default handler.
The service does get started at that point and subsequent sms messages are correctly aborted.

The logging I have in place shows that when the service is NOT running, the BroadcastReceiver_OnReceive Sub is not called, Instead the only segment of the code that gets called is Service_Start. I added Sub ParseSmsIntent to allow me to see what the message body is.
It is only when the service is running that BroadcastReceiver_OnReceive is called

My test was:
Load the app
Stop the service from 'settings'
send the text "stopped" from another phone
send the text "started" from another phone
(then dont forget to uninstall the app or you will never receive another sms)

Result:
The first text is still handled by the default app, second and subsequent are aborted.

--- this is the log when the service was NOT running ---
** Service (bcreceiver) Create **
ServiceCreate
BroadcastReceiver has been initialized.
** Service (bcreceiver) Start **
service_start
into parseSMSintent
Logged in Service_Start: [Address=+64272038977, Body=Stopped, IsInitialized=false]
serviceStart completed


--- this is the log when the service WAS running ---
into BroadcastReceiver
into parseSMSintent
Logged in BroadcastReceiver_OnReceive[Address=+64272038977, Body=Running, IsInitialized=false]
received sms
android.provider.Telephony.SMS_RECEIVED


So in this fictious scenario - how to I abort that first sms !!
And a slightly late Merry Christmas to you.
 

Attachments

  • BCReceiver(1).zip
    8.2 KB · Views: 340
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…