What if the user turns off the power between step 5 and step 6 (or
between step 4 and step 5)?  Or gets a phone call and never returns to
the app?  Or uses Settings to kill the Service?  The point is that
that the application should be designed to expect such normal
conditions and not rely on a series of asynchronous events and user
actions to happen in a predictable order.  This is the way users are
expected to use the mobile device.

On your specific question you might make it a foreground service
(probably a bad idea) or just save the data in a preference (easy but
still will loses the data in some scenarios).

- Paul


On Dec 20, 4:29 pm, Kalyan Akella <[email protected]> wrote:
> Hi,
>
> I have a scenario which requires the Android service from not getting
> destroyed when the Activity that started it (through bindService) calls the
> unbind. Here it goes...
>
> Step1: The main activity A, starts and renders the UI which contains some
> text and a button B.
> Step2. Inside the onCreate() method, A calls bindService and starts/binds to
> a certain service S (which by the way is an IntentService and a
> LocationListener).
> Step3: Back on the UI, on click of the button B, the activity A sends a
> certain message M to the service S.
> Step4: S reads the user's current geo-location and if a fix is available, it
> sends this message M along with this geo-location to a certain web service.
> Step5: But if the current geo-location (the last known location) is
> unavailable or if the web service is unreachable, it queues this message to
> send it across later.
> Step6: On the UI, the user might exit the application which causes A to do
> its onDestroy which in turn calls the unbindService() to unbind from S.
>
> Here's my problem. If the message wasn't sent in Step 5 by the service S and
> in Step 6 if the user exits the application, Android kills the service S
> because there's no one else bound to it and hence the message it was
> supposed to send is forever lost (unless I store it elsewhere to re-send it
> if the user restarts my application).
>
> So, is there a better way to do all this or is there a way to prevent S from
> getting destroyed if the message queue is not empty. Please help.
> Sincere Regards,
> Kalyan

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to