Q:

Getting battery status from a service in Android

I am developing an app for which I need to monitor the remaining battery percentage from a service. Can anybody tell me how to do it? I found some sample codes that are getting the battery status from an activity, not from service.
Thanks.

A:

Use ACTION_BATTERY_CHANGED. It is called every time the battery value changes. 
See the code below to see how it is done:
public void onCreate()
{

this.registerReceiver(this.mBatInfoReceiver, 
        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent intent) {

    int level = intent.getIntExtra("level", 0);
    Log.e("test", String.valueOf(level) + "%");

     }
};

public void onDestroy(){

    // unregister receiver
    this.unregisterReceiver(this.mBatInfoReceiver);
}

A:

What's the difference ?
You should be able to register a receiver for battery events in the service, the same way as you do with an activity.
For example:
private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent intent) {
        int rawLevel = intent.getIntExtra("level", -1);
        int scale = intent.getIntExtra("scale", -1);
        int status = intent.getIntExtra("status", -1);
        // Do something
    }
};

public void onCreate() {
    registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

public void onDestroy() {
     unregisterReceiver(this.mBatInfoReceiver);
}

