I’m new to android programming so these are probably dumb questions. I’ve done some reading but can’t quite get the answer.
I have a broadcast receiver with some intents registered from a service –
receiver = new EventHandler();
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); // for calls
registerReceiver(receiver, filter);
As I’ve removed the “setForeground” call I put in to keep my service running (as I don’t want a status bar icon and I’m wondering if I’m being lazy with that approach), my service will now get killed off regularly and started up again usually a short time later (but sometimes I’ve seen it be 5 mins).
Question – as I’ve registered my intents programatically, how will Android know to send the intents I’m interested in if my process happens to be in the stopped state. I’m assuming I’ll miss some events.
I tried putting the intents in the manifest but I’ve read (and observed) that you can’t use that technique for the battery changed intent.
Thanks.
Ok first of all, if you want to keep your service alive, you have to acquire a
WakeLockfrom thePowerManagerinonCreate()and release it inonDestroy().http://developer.android.com/reference/android/os/PowerManager.html
The OS might still destroy and recreate your service sometimes, but you don’t have to worry about it being stopped for an extended period. (
startForeground()doesn’t guarantee it either, that yourServicewill stay alive after you applied keylock for example.)If your service is destroyed, then the OS will still fire the
Intentsyou just won’t see them.