I need to be able to gain access to objects after my activity has been destroyed (or even after the app has been quit).
The object is an AAC decoder and plays in the background if the user hits home, or stays in the app. However, when the activity/app is exited the object is lost. This means that if the aac stream is playing there is no way to stop it (short of restarting the device) and subsequent visits to the app just layers the streams on top of the last one.
I thought about adding the object to a service, however I have no idea how this would work, or if its even a good idea. I also thought that the answer might lay in static variables, however I assume that once an application has been exited (or force quit/killed for memory/whatever) even these objects are lost.
The library I’m using for the aac decoder is http://code.google.com/p/aacdecoder-android (if that helps).
Currently I’m playing the stream in a service completely. This gets around the problem above, however I need to be able to update the UI when the app is running (and when the user goes to the app in the future). So this is a problem. I realise the way around this would be to use a broadcast intent, but I wonder if its more complicated than necessary.
Is there any way around this?
Thank you.
Not only is it a good idea, but it is your only real option. Here is a sample project showing a (fake) audio client sending commands to a (fake) audio service that handles the playback and indicates to Android that the service is part of the user’s foreground experience.
Once your process is terminated, the object is gone. Of course, so is the audio playback.
A service is effectively a marker, to the OS, indicating that you have code that is still running, so Android is less likely to terminate your process to free up RAM to run other apps. Marking it as part of the foreground user experience further reduces, but does not eliminate, the odds that Android will terminate your process.
It doesn’t get much simpler than a broadcast. It’s not your only option — you could use a
Messengeror aPendingIntentor a handful of other techniques.Why would you want to get “around” the right answer?