Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old November 9th, 2011, 01:40 PM   #1 (permalink)
New Member
 
Join Date: Nov 2011
Posts: 5
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Question How to notify a service from another application

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 .

iZacki is offline  
Reply With Quote
Sponsors
Old November 9th, 2011, 08:19 PM   #2 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

You should be able to both components in the same app.

You should be able to use intents to signal the background service to change refresh rates.

This is all explained in the Services Dev Guide.
jiminaus is offline  
Reply With Quote
Old November 10th, 2011, 01:43 PM   #3 (permalink)
New Member
 
Join Date: Nov 2011
Posts: 5
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Question

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.

iZacki is offline  
Last edited by iZacki; November 10th, 2011 at 03:07 PM.
Reply With Quote
Old November 11th, 2011, 03:00 AM   #4 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

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().
jiminaus is offline  
Reply With Quote
Old November 11th, 2011, 06:55 AM   #5 (permalink)
New Member
 
Join Date: Nov 2011
Posts: 5
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

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.
iZacki is offline  
Reply With Quote
Old November 11th, 2011, 05:57 PM   #6 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

Quote:
Originally Posted by iZacki View Post
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.
jiminaus is offline  
Reply With Quote
Old November 11th, 2011, 06:59 PM   #7 (permalink)
New Member
 
Join Date: Nov 2011
Posts: 5
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
iZacki is offline  
Last edited by iZacki; November 11th, 2011 at 07:01 PM.
Reply With Quote
Old November 11th, 2011, 08:39 PM   #8 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

I've just come from doing years of iOS development. The freedom of having an private app outside of the officially sanctioned software repository is still foreign to me. LOL.
jiminaus is offline  
Reply With Quote
Old November 11th, 2011, 11:31 PM   #9 (permalink)
New Member
 
Join Date: Nov 2011
Posts: 5
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Cool

! That's the reason I'm building mobile apps only on Android and Blackberry. I can't develop with handcuffs! . After using the three development environments, I can say that Android is really the best of all.
iZacki is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:13 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo