1. Download our Official Android App: Forums for Android!

Apps How to notify a service from another application

Discussion in 'Android Development' started by iZacki, Nov 9, 2011.

  1. iZacki

    iZacki Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Nov 9, 2011

    Nov 9, 2011
    5
    0
    5
    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 :).
     

    Advertisement

  2. jiminaus

    jiminaus Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    190
    Joined:
    Oct 27, 2011

    Oct 27, 2011
    190
    36
    63
    Sydney, Australia
    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.
     
  3. iZacki

    iZacki Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Nov 9, 2011

    Nov 9, 2011
    5
    0
    5
    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.

     
  4. jiminaus

    jiminaus Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    190
    Joined:
    Oct 27, 2011

    Oct 27, 2011
    190
    36
    63
    Sydney, Australia
    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().
     
  5. iZacki

    iZacki Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Nov 9, 2011

    Nov 9, 2011
    5
    0
    5
    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.
     
  6. jiminaus

    jiminaus Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    190
    Joined:
    Oct 27, 2011

    Oct 27, 2011
    190
    36
    63
    Sydney, Australia
    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.
     
  7. iZacki

    iZacki Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Nov 9, 2011

    Nov 9, 2011
    5
    0
    5
    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.
     
  8. jiminaus

    jiminaus Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    190
    Joined:
    Oct 27, 2011

    Oct 27, 2011
    190
    36
    63
    Sydney, Australia
    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.
     
  9. iZacki

    iZacki Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Nov 9, 2011

    Nov 9, 2011
    5
    0
    5
    :D! That's the reason I'm building mobile apps only on Android and Blackberry. I can't develop with handcuffs! :rolleyes: . After using the three development environments, I can say that Android is really the best of all.
     

Share This Page

Loading...