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.
Related
Anyone know can activity received the message send by service while activity is in onStop or onPause State? How should I handle so that Activity will received the message send by service while in background? I have an Activity which is received locations send by service from the background. My activity is updating how much user travel every 10 seconds. If the activity is in onPause mode, I don't think activity can do any calculation send by service.
Yes and no. Your activity can't actually receive broadcasts while it's paused or stopped, but you can have your broadcast cause it to be woken back up. But that's probably not something you want to do every 10 seconds. It would likely be better to just keep the knowledge in the service and have the activity ask for it when it gets restarted. If that's an approach you're after I can point you to some examples.
I would recommend you to do the following :
Whatever computation you do, do it in the service. So you will always have the state of user maintained somewhere.
When your activity starts, 'Bind' the service to the activity and call your function of the service FROM the activity to retrieve the state of the user. You can use the 'ServiceConnection' class for this in the activity.
You can call your function of the service from the activity in your 'onResume' function of the activity.
Viewable content should be in the activity and all the other data in the service. When you do the above, the user will see the latest stuff that you want to show. There is no point in sending data to the activity and doing the computation there even if the user does not see it.
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.
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.
I have an IntentSerivce that runs in the background sometimes and it can be quite a long running process in certain instances. I give an option for the user to quit the application which basically just stops and polling and ignores any GCM push notifications. But if a push notification came in and the IntentService is taking a while to finish doing what it has to (gets information from a server and sends a notification to the user if needed, like a new message arrived or something).
Here is the problem, if the user elects to "Quit" the app while the intentservice is still running they will still get the notification which I do not want. I know that there is the stopSelf() method for the service but I need to stop it in an activity when I know the user "Quit" the application via a menu button. Sending another intent to the service does not work since the intents get queued up and calling context.stopService(intent); in my activity does not work either so how else can I stop it?
Are you passing a new Intent into stopService(Intent) or the original one used in startService(Intent). Passing the original Intent should stop the service.
Failing that you could use a Handler to pass the service a Message. Have the IntentService implement the Handler.Callback interface and create a new Handler(Handler.Callback) in your Activity, passing your IntentService as callback. Then implement the onHandleMessage() in your IntentService to call stopSelf() and have your Activity pass a message to it when you want it to stop.
Below code is perfectly working fine for me to stop IntentService from Activity:
stopService(new Intent(getApplicationContext(), MyIntentService.class));
I have a problem:
I have a UI thread which displays webwiew and another chatActivity which displays chat.
I keep on getting data from server which would be displayed on both Activities.
What is the best way to do this viz doInBackground or Service,
If service, than can i bind 2 activity with 1 service i.e. if user press logout from UI or chatActivity, both activity and service should die otherwise service sud update both activity contents.
I am new to 'service' so any reference or sample code would be helpful.
Sounds like you need to broadcast some information. You than will be able to set broadcast receivers in any activity/service you would like to get notified.
Read more online about Broadcastreceiver and about send broadcast