• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to notify a service from another application

iZacki

Lurker
Nov 9, 2011
5
0
Hi,

First, I'm new in the Android application development world.

I have a background service application that get informations through a web service. This service has to alert the user when it gets some abnormal values. I also have a UI application that is used to display these informations. Finally, the only way I found to be able to get the service and the UI application to share these informations, was to create a third application that defines a custom ContentProvider . I've tried to define the ContentProvider in the service application to have only two applications, but it didn't worked. Everything is fine with three applications. If you have a better solution, please let me know!

For CPU and battery saving reasons, the background service is refreshing the informations each 5 minutes. But, the UI application needs to refresh faster than 5 minutes. So, when the UI application starts, I need it to notify the background service to start refreshing at a much faster rate. And, obviously, when it closes, the background service needs to get back to the normal refresh rate.

My problem is that I don't know how to notify the background service from the UI application. I can set a field in the database when the UI application opens and closes, and check that field every second in the background service, but it will kill the CPU and the battery! How can I set some listener in the background service and send and event in the UI application. Can I define custom Broadcast events?

Sorry for my bad english. Thanks for your help :).
 
Thanks jiminaus.

I've been able to regroup all my 3 apps into one and I'm using bindService to talk to my service from my UI. The problem I have now is that the onCreate is called each time I open the UI, but onPause and onDestroy are never called when I hit the back button to close it. A check at the Activity lifecycle on the google site shows that these functions are supposed to be called when an activity is sent to the background or closed. I can bindService, but I can't unbindService since I'm unable to know when the screen is closed.

Maybe my UI activity never closes because my service is now in the same application and that it's always running, but it doesn't explain why the onCreate always get called. Is there any possible reason why the onPause and the onDestroy may never get called?

EDIT : I can trap the UI closing with isFinishing in onStop. Even onRestoreInstanceState and onSaveInstanceState are not called properly. onRestoreInstanceState is called only at the first start. onStart and onStop are called correctly, but onStop doesn't always mean the UI is closed, it may simply have lost the focus.

 
Upvote 0
I can't replicate your behaviour on Android 2.3.3, 3.2 or 4.0 using the simulator (I don't have an Android device yet, waiting for Galaxy Nexus). I only got the full range of expected lifecycle events whether the service was running or not.

However, I didn't use bindService/unbindService. I just used startService and just sent the high refresh rate in the Intent extra data inside onStart() and a low refresh rate inside onStop().
 
Upvote 0
Ok, maybe using the bind and the unbind functions change the lifecycle. In my case, the service is started by the UI only if it has not been started at the boot by a BroadcastReceiver. So I never stop it. It can only be stopped by the OS is there's something wrong.

For now, my tests were done without rebooting my phone, so the service was always started by the UI activity. I'll do more tests to see if the lifecycle is different when the service is started by the BroadcastReceiver instead of the UI activity.

Thanks for your help.
 
Upvote 0
Ok, maybe using the bind and the unbind functions change the lifecycle. In my case, the service is started by the UI only if it has not been started at the boot by a BroadcastReceiver. So I never stop it. It can only be stopped by the OS is there's something wrong.

For now, my tests were done without rebooting my phone, so the service was always started by the UI activity. I'll do more tests to see if the lifecycle is different when the service is started by the BroadcastReceiver instead of the UI activity.

Thanks for your help.

You really should allow the user to control the service. I wouldn't want your service using up the last of my data allowance or my battery.
 
Upvote 0
This application has been built to be used like that. The informations it provides is way more important than a phone battery. This application will never hit the market since it has been built based on our specific business needs.

My application is working perfectly.

Thanks for your help.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones