0

I would like to track battery information(is it charging, level etc.) even when my app is not turned on. I think maybe service would be good for it? Or maybe there are other solutions? I'm all ears up.

whiteLT
  • 338
  • 7
  • 21
  • you can use a service to fire an ACTION_BATTERY_CHANGED intent periodically and read the data as shown in my [answer here](http://stackoverflow.com/questions/14805318/android-batterymanager-returns-only-1/14805410#14805410) – Robin Chander Feb 28 '13 at 19:40
  • @robin great, could you suggest me how often I should fire that intent, and maybe you have some code examples of service? Would be very helpful :) – whiteLT Feb 28 '13 at 19:50

2 Answers2

1

A service needs to be declared in the AndroidManifest.xml and the implementing class must extend the Service class or one of its subclasses. The following code shows an example for a service declaration and its implementation.

<service
  android:name="MyService"
  android:icon="@drawable/icon"
  android:label="@string/service_name"
  >
</service> 

public class MyService extends Service {

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
    return Service.START_NOT_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
  //TODO for communication return IBinder implementation
    return null;
  }
} 
whiteLT
  • 338
  • 7
  • 21
1

This is in fact a very old post but since the accepted answer seems like a stub service i think there is the need to go deeper.

You need to declare the service in the android manifest. If you have it in a certain package the declaration would be:

<service android:name="packagename.servicename" />

Then you would need to implement the service class. I have created one for one of my applications. Hope this helps you.

I have this in the context of sensors. sensors are something i want to monitor so i created an interface like the following:

public interface Sensor {

    SensorType getSensorType();

    SensorName getSensorName();

}

I have to ENUM classes that represent the types and names of sensors i have:

public enum SensorName {
    ENVIRONMENTAL_SENSOR, AUDIO_SENSOR, SOUND_SENSOR, SOCIAL_SENSOR,
    LOCATION_SENSOR, PHOTO_SENSOR, ACCELEROMETER_SENSOR, BATTERY_SENSOR;
}

public enum SensorType {
    HARDWARE_SENSOR, SOFTWARE_SENSOR, HYBRID_SENSOR;
}

Since i have several sensors and i might want to have a listing of all the sensors i needed to implement this interface. Next i created a abstract class to implement behaviours that are similar to all my sensors.

public abstract class SensorElement extends Service implements Sensor{

    protected SensorType type;
    protected SensorName name;

    @Override
    public abstract IBinder onBind(Intent arg0);

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public abstract void onCreate();

    @Override
    public void onDestroy(){
        super.onDestroy();
    }
}

My sensors need this methods to work properly. In my case i only absolutely need the binder to bind the service to my activity.

Now what you want, the battery sensor itself:

public class BatterySensor extends SensorElement {

    // Binder given to clients
    private final IBinder mBinder = new BatteryLocalBinder();

    /**
     * Class used for the client Binder. Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class BatteryLocalBinder extends Binder {
        public BatterySensor getService() {
            // Return this instance of MotionLightOrientationSensor so clients
            // can call
            // public
            // methods
            return BatterySensor.this;
        }
    }

    private static final String TAG = "BatterySensor";
    public static final SensorType type = SensorType.SOFTWARE_SENSOR;
    public static final SensorName name = SensorName.BATTERY_SENSOR;

    private int status;
    private boolean isCharging;
    private int chargePlug;
    private boolean usbCharge;
    private boolean acCharge;
    private int level;
    private int scale;
    private double batteryPct;
    private Battery battery;

    @Override
    public SensorType getSensorType() {
        return type;
    }

    @Override
    public SensorName getSensorName() {
        return name;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(powerconnectionreceiver, ifilter);
        battery = new Battery();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        unregisterReceiver(powerconnectionreceiver);
    }

    private BroadcastReceiver powerconnectionreceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Retrieves a map of extended data from the intent.
            status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
                    || status == BatteryManager.BATTERY_STATUS_FULL;

            chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
            acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            batteryPct = level / (float) scale;
            battery.setStatus(status);
            battery.setCharging(isCharging);
            battery.setChargePlug(chargePlug);
            battery.setUsbCharge(usbCharge);
            battery.setAcCharge(acCharge);
            battery.setLevel(level);
            battery.setScale(scale);
            battery.setBatteryPct(batteryPct);

        }

    };

    public Battery getBatteryReading() {
        return this.battery;
    }

    public int getStatus() {
        return status;
    }

    public boolean isCharging() {
        return isCharging;
    }

    public int getChargePlug() {
        return chargePlug;
    }

    public boolean isUsbCharge() {
        return usbCharge;
    }

    public boolean isAcCharge() {
        return acCharge;
    }

    public int getLevel() {
        return level;
    }

    public int getScale() {
        return scale;
    }


}

To access data from this sensor you only need to start it in you activity like this:

private BatterySensor batterySensor;
private boolean mBatteryBound = false;
private Intent intentBattery;

intentBattery = new Intent(this.context, BatterySensor.class);

then i start the service:

context.startService(intentBattery);

and bind it:

if(this.context.bindService(intentBattery, mBatteryConnection, Context.BIND_AUTO_CREATE)){
            this.numBoundedSensors++;
        }

to bind it you need to have this class in your activity:

/** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mBatteryConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            BatteryLocalBinder BatteryBinder = (BatteryLocalBinder) service;
            batterySensor = BatteryBinder.getService();
            mBatteryBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBatteryBound = false;
        }
    };

Then if it succeeded the bind you can call all the methods from the sensor, like this:

public Battery getBatteryData(){
        return mBatteryBound ? batterySensor.getBatteryReading() : null;
}