package com.hong.monitor.battery;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
public class BatteryMonitorActivity extends Activity {
private CheckBox ac,usb,charging;
private TextView battery_level;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = getApplicationContext().registerReceiver(null, ifilter);
// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
// How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
ac = (CheckBox) findViewById(R.id.ac);
usb = (CheckBox) findViewById(R.id.usb);
charging = (CheckBox) findViewById(R.id.charge);
ac.setChecked(acCharge);
usb.setChecked(usbCharge);
charging.setChecked(isCharging);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
int temp = batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
int voltage = batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
char c=186;
int health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
String shealth;
switch(health) {
/*
case BatteryManager.BATTERY_HEALTH_COLD: // for level 11
shealth = "Battery Cold";
break;
*/
case BatteryManager.BATTERY_HEALTH_DEAD:
shealth = "Battery Dead/";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
shealth = "Battery Good/";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
shealth = "Battery Overheat/";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
shealth = "Battery Over Voltage/";
break;
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
shealth = "Battery Unknown/";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
shealth = "Battery Unspecified failure/";
break;
default:
shealth = "Battery Other/";
}
battery_level = (TextView) findViewById(R.id.bat_level);
battery_level.setText(shealth + level + "%/" + temp/10.0 + c + "C/" + voltage + "mV");
}
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
ac.setChecked(acCharge);
usb.setChecked(usbCharge);
charging.setChecked(isCharging);
if(isCharging)
Log.i("PowerConnectionReceiver", "Charging!");
else
Log.i("PowerConnectionReceiver", "Not Charging!");
if(usbCharge)
Log.i("PowerConnectionReceiver", "USB Charging!");
if(acCharge)
Log.i("PowerConnectionReceiver", "AC Charging!");
Toast.makeText(getApplicationContext(),"ChargePlug: " + chargePlug,Toast.LENGTH_SHORT).show();
}
}
public class BatteryLevelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent battery) {
int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
Log.i("BatteryLevelReceiver", "Battery Level: " + batteryPct);
Toast.makeText(getApplicationContext(),"Battery Level:" + batteryPct,Toast.LENGTH_SHORT).show();
}
}
}
沒有留言:
張貼留言