Reputation: 2184
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
Upvotes: 0
Views: 1209
Reputation: 19800
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
.
Upvotes: 3