0

I'm coding for app,where i just want add a feature i.e when battery level is getting low(say for less than 20%),app will automatically get stopped and respective services also get closed ,is it possible?if so please give some methods to do so..Thanks in advance

immutable
  • 2,174
  • 3
  • 20
  • 27
  • It would be possible but you may want to rethink this. If it just closes, your users may think its a bug. Before it gets to the critical point, you may want to warn the users instead – codeMagic Mar 01 '13 at 13:40
  • @codeMagic ya i will surely show a alert about the battery status,but is it possible to make app terminate itself? – immutable Mar 01 '13 at 13:50
  • Of course you can just finish any open `Activities`. I don't know what you have going on but just keep track of any running services that you need to stop or anything like that – codeMagic Mar 01 '13 at 13:52

1 Answers1

3

You should listen to the Intent "android.intent.action.ACTION_BATTERY_LOW" with an BroadcastReciever

You can stop your components via: Add the BroadcastReceiver via code in your Services and Activities. It will receive the Intent where you can stop the components: Activity has finish(), Service has stopSelf(). This will only stop the components which have been running before! (a good thing)

Or you can do it via BroadcastReceiver via manifest: When you receive it in the BroadcastReceiver.onReceive you can call stopService to stop your Service. To stop the Activity is a lot harder, check if your Activity is in the foreground, send a custom Intent to it. It will receive it and can stop itself. Prerequisite is that it is singleInstance.

RvdK
  • 19,580
  • 4
  • 64
  • 107