send broadcast message when service is stopped - android

I would like to send the broadcast message back to the activity when the service is stopped by the user or OS. I have tried to add the code in onDestroy of the service while onDestroy have not been called every time.
Can anyone give me suggestion how I can send the message when the service is stopped?

Try using StartSticky and sending a broadcast every time the service start(onStart). So when it is destroyed, the service will try to run again by itself and will send the broadcast. This way you will know when the server have been closed and open again.
P.D: Remember to send the broadcast in the onStart, not in onCreate.(Service Lifecycle.)

Related

how do i stop a service started by a broadcast receiver

I have a broadcast receiver that listens for an sms or call from a specific number. when it triggers it starts a location service which runs from that point on.
I need to be able to stop this service once it is no longer needed from my app.
My app does not need to be running for this broadcast receiver to trigger the service.
Any advice would be very much appreciated
Thank You

Sending broadcast from the service to killed activity

i am working on my first android application and what it does is that it has an activity which after signing in starts a service that pings user location to the server after a certain time period or whenever user location changes. Now for the communication from the service to the activity I am using BroadcastReceiver.
The service after logging the location to the server broadcasts a message which my activity receives and update the UI accordingly. Now everything is good up till this point but when I delete my activity from the background while my service is still running in the background what happens is that whenever the service logs the location on the server and broadcasts the message it crashes and a message is shown on the screen saying your application has been stopped.
I think that the android broadcast system is expecting my activity to be alive to receive the broadcast and when it doesn't find it then it crashes the service.
What I want is that if my activity is around it will receive the broadcast but if it is not then the broadcast should be ignored. It should not crash my service.
I am following proper steps by registering for the service in onResume() and unregistering in onPause(). Moreover I am registering the service dynamically rather than in the manifest.
you should use bindService(intent) not startservice(intent) for indefinite internal of time.so it would work till your activity is binded to your service when you finish activity then call onUnbind().
onDestroy()in activity call stop service.
check in manifest that your receiver is not registered there.
I was doing a stupid mistake there. I was passing the references of variables defined in my activity to the broadcast service. It worked fine when my activity is still alive but when my activity was killed the service ended up with null pointers which upon updating crashed 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

Handling Pending Notifications with Result Receiver in Activity Service communication

Hi stackoverflow community. This is my first time asking a question so please forgive me if I have some errors in this post
I am trying to communicate an Activity with an Intent Service using the Result Receiver pattern explained in this post and discussed here in stackoverflow. Eveything is working OK in a sample app but I am not sure if this pattern is effective when Activity goes to background or gets killed by Android OS.
Basically, what I want to achieve is the following app behavior:
Activity: Starts IntentService to do some work. Passes ResultReceiver to receive progress updates
IntentService: Starts working, get Activity's ResultReceiver.
IntentService: Work is completed in 10%, notifies Activity by sending message to Result Receiver
Activity: Receives message in onReceiveResult
IntentService: Work is completed in 40%, notifies Activity by sending message to Result Receiver
Activity: Receives message in onReceiveResult
At this point, user goes to another app. Activity goes to background dettaching first the Result Receiver. Android OS could also have destroyed this Activity because of an incomming call
Intent Service: Work complete. Tries to notify Activity but no Result Receiver available. Saves message for later redelivery to Activity.
At this point, user gets back to the app. Activity goes to foreground, attaches Result Receiver and gets pending message from Intent Service that notifies that work is done.
So, my question is how to handle pending notifications of an IntentService to the Activity using the Result Receiver pattern?
Thank you very much.

Android Service restarts randomly

I'm trying to write a Service which sits and waits for SMS messages (using a BroadcastReceiver which the server registers) and will take some action depending on the details of the SMS message. I use an Activity to startService() and stopService() for the Service. After I close the Activity, the Service continues to sit there with its state kept and its BroadcastReceiver waiting, as it should... However, I find that over time, randomly, the Service will restart itself. That is, onCreate() and onStartCommand() will be called even when the Service has already been started. This happens sometimes when my phone sits idle overnight...
Can anyone provide insight on why this is (phone requests resources and kills a service?), what I can do to prevent it, or what I can do to prevent the state of the service from being lost?
Thanks in advance.
I don't think you need to start service from activity at all. Have a broadcast receiver that listens to your SMS messages and starts the service. After work is done, service should call stopSelf();
I'll try Service.startForeground()
Android sometimes (when it's low on resources, mem/cpu) removes running services and once it's high on resources again brings them back. This is why you see that your services has been restarted a couple of times during night.

Categories

Resources