android - SMS Receive Broadcast receiver getting aborted - android

I am developing an android SMS app which will not save the message in the inbox.
I need to verify the content of the message using a webservice call and then remove the broadcast.
In the onReceive() for SMSReceiver
if (messageBody.equals("somedata"))
{
Intent i = new Intent(context,Webservice.class);//webservice validation
i.putExtra("messageBody",messageBody)
startService(i);
// Stop it being passed to the main Messaging inbox
abortBroadcast();
}
The Intentservice for webservice call never gets executed.The receiver is aborted before this.
I am new to android,so please let me know if it right to do the webservice call in an intent service started in the onReceive() of the broacast receiver(SMS_RECEIVE).

Use logging to be sure that the equals() returns true. If that is the case, it might be that you're doing something time consuming in the onReceive() and the BroadcastReceiver gets killed. From documentation:
http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive(android.content.Context, android.content.Intent)
The function is normally called within the main thread of its process, so you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed).

It may be spaces problem in your messageBody variable,
please try this,
if (messageBody.trim().equals("somedata"))

Related

Is it correct to call WebServices inside onReceive of BroadcastReceiver?

I've a BroadcastReciever on a separate class, i register that receiver on one of the activities, and this broadcast receiver trigger when there is an Internet connectivity. Inside onReceive() of the receiver, i execute a method for getting token from the server.
But when i gone through the documentation i found that; "When it runs on the main thread you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed)."
Please help me with the correct way of doing it.
IMO , it is perfectly fine to call a WebService inside the onReceive method of a broadcast receiver. I have done it in many of my applications and till now I have never faced any problem.
Infact in majority of applications, which require frequent updates from server the BroadcastReceiver component is used as the onReceive method runs in the Worker thread/task.
To be on the safe side you can set your WebService timeout to less than 10 secs.Another implementation can be that you can create a background/worker thread, send it a token from the onReceive of your BroadcastReceiver and inside that thread you can call your WebService.
No, The Android System may be kills the your BroadcastReceiver, in case of in-sufficient memory. Because user never or not recently interact with the process of Application.
A process that is currently executing a BroadcastReceiver (that is, currently running the code in its onReceive(Context, Intent) method) is considered to be a foreground process and will be kept running by the system except under cases of extreme memory pressure.
That means A Process holds only BroadcastReceiver, then it may be considered as low priority under cases of extreme memory pressure.
This means that for longer-running operations you will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation.
Intent intent = new Intent(mContext, MyService.class)
intent.setData(Uri.parse(your_url));
mContext.startService(intent);

In Android, what's the best way to communicate between a service and a BroadcastReceiver?

I need to be able to have a service "tell" a BroadcastReceiver to do some work, and then wait until that work is done, and once it's finished, the BroadcastReceiver needs to send the result back to the service. And at that point, the service can continue execution.
So, "sending" the work from the service to the BroadcastReceiver is easy -- I just send a broadcast with the intent using extras for the work that needs to be done. But I don't know how to have the BroadcastReceiver then send the results back to the service. What's the best way to do this?
Edit:
I should have mentioned that the service in question is actually a contact sync adapter service. I'm not sure if that makes a difference, but maybe it does. Some of the work can't be done in the scope of the sync adapter, and that's why I'm offloading it to be done by the broadcastreceiver.
Register a Second inline Broadcast Receiver from the Service just to receive the results from the First Broadcast receiver. Pass the data via Intent. In the Second Broadcast receivers onReceive() do whatever you want.
Unregister the Second Broadcast Receiver in onDestroy of the Service.

Relationship between broadcast receiver and Service

Im confused with Service and Broadcast receiver.what is the relationship between these two?why we have to call broadcast receiver when we start a service.Can anyone kindly explain the concept between these two elements
You don't have to register a BroadcastRecevier when you start a Service. That is, even if you don't register a BroadcastReceiver, our Service will work as expected. There is no must have dependency between the two.
As explained by Gridtestmail, a Service is a process you want to run in the background, without having an interface to the user.
A BroadcastReceiver is registered, when you want to be notified about certain events happening - for example, discovering a new bluetooth device or receiving an incoming call.
If you register a BroadcastReceiver for receiving incoming calls , then your Receiver's onReceive() method is called whenever there is an incoming all, so you can process it.
Similarly, for other event detection stuff.
I hope the concept is clear to you now.
Service: If you want to do something in background , this will be running always in background even if the application closed. You can create this in separate process and also you can give your service to other app if you want. Downloading any content or Music is good example
Broadcast Reciever: Usually system will send some info which can be recieved by your app if you would wish to ,by registering. And you can do something what you want when that thing happens by using onReceive method. Example is the system will send BroadcastReceiver when new sms arrives or Booting done
Example : Service and BroadcastReceiver

Message from Service to Activity never arrives in Handler

it seems I missed something important:
Until now I had an activity that starts an own thread. This thread sends messages from time to time using the sendMessage() method of a Handler that is located in same activity. That works fine.
Now I added a service doing some cool stuff in background. This Service itself tries to send some messages using the same way: calling sendMessage() of the Handler that is located in Activity. But these messages arrive only sporadically, many of them get lost.
So it seems there is something different in Messages/Handlers when a Service is involved?
Thanks!
It's better if you declare a local broadcast receiver in your activity to receive the information of the service, and then, in your receiver you can send the message to the handler.

Regarding BroadCast Receiver and Service

The application is being developed in 2.3.3
In my application, I am starting a service from a broadcast receiver. The service calls the Location Manager and gets latitude and longitude. Is it possible to send the latitude and longitude values back to broadcast receiver from Service?
If Yes? How do we do it?
If No? -> which is my approach as of now... I have used a SharedPreferences object to hold these values and access them in BroadCast Receiver. Will this work?
I do have few more questions on this, will edit my question after the answers...
Thanks..
[Edit] Answer to my above question -> So we can send data from Service to Broadcast Receiver, though it's not a good practice in general.
It is possible to pass the information back to the broadcast receiver by broadcasting a new intent from the service that has the location data. You will need to create a custom action for the intent and have your broadcast receiver register to handle that action. Creating and receiving intents with custom actions is described here.
However, I think in pretty much every case this is a bad approach. In general broadcast receivers should merely parse intents and launch intent services to do all of the work. If something needs to be done with the location then the service should do it not the broadcast receiver. It is also important to note that at the end of onReceive() the broadcast receiver is immediately destroyed so you lose all of its state.
I'd be happy to answer more questions or update my answer to be more specific if you provided more information as to the purpose of lat/long gathering task.
[EDIT] Since I can't comment I'll add my two cents to Kerim's answer. Unless I am missing something huge (if I am please let me know Kerim since you can comment on mine) may not work the way you expect. When the broadcast receiver's onReceive() function finishes the broadcast receiver will be destroyed. The handler you sent will live on but any data in the broadcast receiver will not exist and the handler will not be able to call the broadcast receiver's functions. Since the handler can't access any of the broadcast receiver's data or functions, you might as well do everything that needs to be done in the service.
You can use Messenger class to communicate accross processes.
When you start a Service, pass a Messenger object as an extra. Then service after completing its job, can send latitude and longitude values back by calling messenger.send(message);
Messenger class takes a Handler object in its constructor. Any Message objects sent through this Messenger will appear in the Handler as if Handler.sendMessage(message) had been called directly.
Here is nice tutorial that explains in detail: http://www.vogella.com/articles/AndroidServices/article.html#servicecommunication_handler

Categories

Resources