Detect Battery Info.

The app simple detect battery level, voltage, temperature, technology, charging status, and health of the battery.



Activity


 public class MainActivity extends Activity {

    private TextView batteryLevel, batteryVoltage, batteryTemperature,
            batteryTechnology, batteryStatus, batteryHealth;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        batteryLevel = (TextView) findViewById(R.id.batterylevel);
        batteryVoltage = (TextView) findViewById(R.id.batteryvoltage);
        batteryTemperature = (TextView) findViewById(R.id.batterytemperature);
        batteryTechnology = (TextView) findViewById(R.id.batterytechology);
        batteryStatus = (TextView) findViewById(R.id.batterystatus);
        batteryHealth = (TextView) findViewById(R.id.batteryhealth);

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

    private BroadcastReceiver myBatteryReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub

            if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
                batteryLevel.setText("Level: "
                        + String.valueOf(arg1.getIntExtra("level", 0)) + "%");
                batteryVoltage
                        .setText("Voltage: "
                                + String.valueOf((float) arg1.getIntExtra(
                                        "voltage", 0) / 1000) + "V");
                batteryTemperature.setText("Temperature: "
                        + String.valueOf((float) arg1.getIntExtra(
                                "temperature", 0) / 10) + "c");
                batteryTechnology.setText("Technology: "
                        + arg1.getStringExtra("technology"));

                int status = arg1.getIntExtra("status",
                        BatteryManager.BATTERY_STATUS_UNKNOWN);
                String strStatus;
                if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
                    strStatus = "Charging";
                } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
                    strStatus = "Dis-charging";
                } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
                    strStatus = "Not charging";
                } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
                    strStatus = "Full";
                } else {
                    strStatus = "Unknown";
                }
                batteryStatus.setText("Status: " + strStatus);

                int health = arg1.getIntExtra("health",
                        BatteryManager.BATTERY_HEALTH_UNKNOWN);
                String strHealth;
                if (health == BatteryManager.BATTERY_HEALTH_GOOD) {
                    strHealth = "Good";
                } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {
                    strHealth = "Over Heat";
                } else if (health == BatteryManager.BATTERY_HEALTH_DEAD) {
                    strHealth = "Dead";
                } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
                    strHealth = "Over Voltage";
                } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {
                    strHealth = "Unspecified Failure";
                } else {
                    strHealth = "Unknown";
                }
                batteryHealth.setText("Health: " + strHealth);

            }
        }

    };
}

XML activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="hello"
    />
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Battery Info."
    />
<TextView 
    android:id="@+id/batterylevel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Battery Level:"
    />
<TextView 
    android:id="@+id/batteryvoltage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Battery Voltage:"
    />
<TextView 
    android:id="@+id/batterytemperature"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Battery Temperature:"
    />
<TextView 
    android:id="@+id/batterytechology"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Technology:"
    />
<TextView 
    android:id="@+id/batterystatus"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Status:"
    />
<TextView 
    android:id="@+id/batteryhealth"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Health:"
    />
</LinearLayout>



Detect Battery Info. Detect Battery Info. Reviewed by Nirav Ranpara on 10:54 PM Rating: 5

Random Posts

My Android Tuts. Powered by Blogger.