Disable update on battery percentage
Posted
by
Kris B
on Stack Overflow
See other posts from Stack Overflow
or by Kris B
Published on 2012-03-18T17:34:54Z
Indexed on
2012/03/18
17:55 UTC
Read the original article
Hit count: 166
I have a service that performs background updates. I want to give the user the the option to disable the updates when their battery percentage reaches a certain level. From my research, I'm going to use a receiver in the onCreate method of my Service class, eg:
public class MainService extends Service
{
@Override
public void onCreate()
{
this.registerReceiver(this.BatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver BatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
int level = intent.getIntExtra("level", 0);
}
};
}
I'm assuming the best practice is to leave the service running and check the battery level in the service and not perform the CPU intensive code based on the percentage? I don't actually stop the service itself and start it up again, based on the battery percentage?
© Stack Overflow or respective owner