package com.example.batteryprop;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.format.DateUtils;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView mStatus;
private TextView mPower;
private TextView mLevel;
private TextView mScale;
private TextView mHealth;
private TextView mVoltage;
private TextView mTemperature;
private TextView mTechnology;
private TextView mUptime;
private static final int EVENT_TICK = 1;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case EVENT_TICK:
updateBatteryStats();
sendEmptyMessageDelayed(EVENT_TICK, 1000);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.battery_info);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mStatus = (TextView) findViewById(R.id.status);
mPower = (TextView) findViewById(R.id.power);
mLevel = (TextView) findViewById(R.id.level);
mScale = (TextView) findViewById(R.id.scale);
mHealth = (TextView) findViewById(R.id.health);
mTechnology = (TextView) findViewById(R.id.technology);
mVoltage = (TextView) findViewById(R.id.voltage);
mTemperature = (TextView) findViewById(R.id.temperature);
mUptime = (TextView) findViewById(R.id.uptime);
mHandler.sendEmptyMessageDelayed(EVENT_TICK, 1000);
registerReceiver(mIntentReceiver, mIntentFilter);
}
@Override
public void onPause() {
super.onPause();
mHandler.removeMessages(EVENT_TICK);
// we are no longer on the screen stop the observers
unregisterReceiver(mIntentReceiver);
}
private void updateBatteryStats() {
long uptime = SystemClock.elapsedRealtime();
mUptime.setText(DateUtils.formatElapsedTime(uptime / 1000));
}
/**
* Format a number of tenths-units as a decimal string without using a
* conversion to float. E.g. 347 -> "34.7", -99 -> "-9.9"
*/
private final String tenthsToFixedString(int x) {
int tens = x / 10;
// use Math.abs to avoid "-9.-9" about -99
// 347/10 = 34 34+.+7 = 34.7
return Integer.toString(tens) + "." + Math.abs(x - 10 * tens);
}
/**
* Listens for intent broadcasts
*/
private IntentFilter mIntentFilter;
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
mLevel.setText("" + intent.getIntExtra("level", 0));
mScale.setText("" + intent.getIntExtra("scale", 0));
mVoltage.setText("" + intent.getIntExtra("voltage", 0) + " "
+ getString(R.string.battery_info_voltage_units));
mTemperature.setText(""
+ tenthsToFixedString(intent.getIntExtra("temperature",
0))
+ getString(R.string.battery_info_temperature_units));
mTechnology.setText("" + intent.getStringExtra("technology"));
System.out.println("yanglei _technology" + intent.getStringExtra("technology"));
mStatus.setText(getBatteryStatus(getResources(), intent));
switch (plugType) {
case 0:
mPower.setText(getString(R.string.battery_info_power_unplugged));
break;
case BatteryManager.BATTERY_PLUGGED_AC:
mPower.setText(getString(R.string.battery_info_power_ac));
break;
case BatteryManager.BATTERY_PLUGGED_USB:
mPower.setText(getString(R.string.battery_info_power_usb));
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
mPower.setText(getString(R.string.battery_info_power_wireless));
break;
case (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB):
mPower.setText(getString(R.string.battery_info_power_ac_usb));
break;
default:
mPower.setText(getString(R.string.battery_info_power_unknown));
break;
}
int health = intent.getIntExtra("health",
BatteryManager.BATTERY_HEALTH_UNKNOWN);
String healthString;
if (health == BatteryManager.BATTERY_HEALTH_GOOD) {
healthString = getString(R.string.battery_info_health_good);
} else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {
healthString = getString(R.string.battery_info_health_overheat);
} else if (health == BatteryManager.BATTERY_HEALTH_DEAD) {
healthString = getString(R.string.battery_info_health_dead);
} else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
healthString = getString(R.string.battery_info_health_over_voltage);
} else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {
healthString = getString(R.string.battery_info_health_unspecified_failure);
} else if (health == BatteryManager.BATTERY_HEALTH_COLD) {
healthString = getString(R.string.battery_info_health_cold);
} else {
healthString = getString(R.string.battery_info_health_unknown);
}
mHealth.setText(healthString);
}
}
};
public String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
final Intent intent = batteryChangedIntent;
int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
System.out.println("yanglei _status" + status);
String statusString;
if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
int resId;
if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
resId = R.string.battery_info_status_charging_ac;
} else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
resId = R.string.battery_info_status_charging_usb;
} else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
resId = R.string.battery_info_status_charging_wireless;
} else {
resId = R.string.battery_info_status_charging;
}
statusString = res.getString(resId);
} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
statusString = res
.getString(R.string.battery_info_status_discharging);
} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
statusString = res
.getString(R.string.battery_info_status_not_charging);
} else if (status == BatteryManager.BATTERY_STATUS_FULL) {
statusString = res.getString(R.string.battery_info_status_full);
} else {
statusString = res.getString(R.string.battery_info_status_unknown);
}
return statusString;
}
}
上Activity中调用了系统类,但是API没有这个里,可以在eclipse里面虚拟一个,运行时会优先调用系统的
建立一个包:android.os
在包下虚拟一个类
package android.os;
import android.os.RemoteException;
/**
* The BatteryManager class contains strings and constants used for values
* in the {@link android.content.Intent#ACTION_BATTERY_CHANGED} Intent, and
* provides a method for querying battery and charging properties.
*/
public class BatteryManager {
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* integer containing the current status constant.
*/
public static final String EXTRA_STATUS = "status";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* integer containing the current health constant.
*/
public static final String EXTRA_HEALTH = "health";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* boolean indicating whether a battery is present.
*/
public static final String EXTRA_PRESENT = "present";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* integer field containing the current battery level, from 0 to
* {@link #EXTRA_SCALE}.
*/
public static final String EXTRA_LEVEL = "level";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* integer containing the maximum battery level.
*/
public static final String EXTRA_SCALE = "scale";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* integer containing the resource ID of a small status bar icon
* indicating the current battery state.
*/
public static final String EXTRA_ICON_SMALL = "icon-small";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* integer indicating whether the device is plugged in to a power
* source; 0 means it is on battery, other constants are different
* types of power sources.
*/
public static final String EXTRA_PLUGGED = "plugged";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* integer containing the current battery voltage level.
*/
public static final String EXTRA_VOLTAGE = "voltage";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* integer containing the current battery temperature.
*/
public static final String EXTRA_TEMPERATURE = "temperature";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* String describing the technology of the current battery.
*/
public static final String EXTRA_TECHNOLOGY = "technology";
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* Int value set to nonzero if an unsupported charger is attached
* to the device.
* {@hide}
*/
public static final String EXTRA_INVALID_CHARGER = "invalid_charger";
// values for "status" field in the ACTION_BATTERY_CHANGED Intent
public static final int BATTERY_STATUS_UNKNOWN = 1;
public static final int BATTERY_STATUS_CHARGING = 2;
public static final int BATTERY_STATUS_DISCHARGING = 3;
public static final int BATTERY_STATUS_NOT_CHARGING = 4;
public static final int BATTERY_STATUS_FULL = 5;
// values for "health" field in the ACTION_BATTERY_CHANGED Intent
public static final int BATTERY_HEALTH_UNKNOWN = 1;
public static final int BATTERY_HEALTH_GOOD = 2;
public static final int BATTERY_HEALTH_OVERHEAT = 3;
public static final int BATTERY_HEALTH_DEAD = 4;
public static final int BATTERY_HEALTH_OVER_VOLTAGE = 5;
public static final int BATTERY_HEALTH_UNSPECIFIED_FAILURE = 6;
public static final int BATTERY_HEALTH_COLD = 7;
// values of the "plugged" field in the ACTION_BATTERY_CHANGED intent.
// These must be powers of 2.
/** Power source is an AC charger. */
public static final int BATTERY_PLUGGED_AC = 1;
/** Power source is a USB port. */
public static final int BATTERY_PLUGGED_USB = 2;
/** Power source is wireless. */
public static final int BATTERY_PLUGGED_WIRELESS = 4;
/** @hide */
public static final int BATTERY_PLUGGED_ANY =
BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS;
/*
* Battery property identifiers. These must match the values in
* frameworks/native/include/batteryservice/BatteryService.h
*/
/** Battery capacity in microampere-hours, as an integer. */
public static final int BATTERY_PROPERTY_CHARGE_COUNTER = 1;
/**
* Instantaneous battery current in microamperes, as an integer. Positive
* values indicate net current entering the battery from a charge source,
* negative values indicate net current discharging from the battery.
*/
public static final int BATTERY_PROPERTY_CURRENT_NOW = 2;
/**
* Average battery current in microamperes, as an integer. Positive
* values indicate net current entering the battery from a charge source,
* negative values indicate net current discharging from the battery.
* The time period over which the average is computed may depend on the
* fuel gauge hardware and its configuration.
*/
public static final int BATTERY_PROPERTY_CURRENT_AVERAGE = 3;
/**
* Remaining battery capacity as an integer percentage of total capacity
* (with no fractional part).
*/
public static final int BATTERY_PROPERTY_CAPACITY = 4;
/**
* Battery remaining energy in nanowatt-hours, as a long integer.
*/
public static final int BATTERY_PROPERTY_ENERGY_COUNTER = 5;
}
battery_info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<!-- Battery Status -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/battery_info_status_label"
android:textSize="12sp"
android:textStyle="normal" />
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="normal" />
</LinearLayout>
<!-- Battery Status -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/battery_info_power_label"
android:textSize="12sp"
android:textStyle="normal" />
<TextView
android:id="@+id/power"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="normal" />
</LinearLayout>
<!-- Battery Level -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/battery_info_level_label"
android:textSize="12sp"
android:textStyle="normal" />
<TextView
android:id="@+id/level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="normal" />
</LinearLayout>
<!-- Battery Scale -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/battery_info_scale_label"
android:textSize="12sp"
android:textStyle="normal" />
<TextView
android:id="@+id/scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="normal" />
</LinearLayout>
<!-- Battery Health -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/battery_info_health_label"
android:textSize="12sp"
android:textStyle="normal" />
<TextView
android:id="@+id/health"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="normal" />
</LinearLayout>
<!-- Battery Voltage -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/battery_info_voltage_label"
android:textSize="12sp"
android:textStyle="normal" />
<TextView
android:id="@+id/voltage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="normal" />
</LinearLayout>
<!-- Battery Temperature -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/battery_info_temperature_label"
android:textSize="12sp"
android:textStyle="normal" />
<TextView
android:id="@+id/temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="normal" />
</LinearLayout>
<!-- Battery Technology -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/battery_info_technology_label"
android:textSize="12sp"
android:textStyle="normal" />
<TextView
android:id="@+id/technology"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="normal" />
</LinearLayout>
<!-- Uptime -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/battery_info_uptime"
android:textSize="12sp"
android:textStyle="normal" />
<TextView
android:id="@+id/uptime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="normal" />
</LinearLayout>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">BatteryProp</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="battery_info_status_label">"电池状态:"</string>
<string name="battery_info_power_label">"是否已插电:"</string>
<string name="battery_info_scale_label">"电池电量范围:"</string>
<string name="battery_info_level_label">"电池电量:"</string>
<string name="battery_info_health_label">"电池运行状况:"</string>
<string name="battery_info_technology_label">"电池技术:"</string>
<string name="battery_info_voltage_label">"电池电压:"</string>
<string name="battery_info_voltage_units">"mV"</string>
<string name="battery_info_temperature_label">"电池温度:"</string>
<string name="battery_info_temperature_units">"° C"</string>
<string name="battery_info_uptime">"启动后的时间:"</string>
<string name="battery_info_health_unknown">"未知"</string>
<string name="battery_info_health_good">"正常"</string>
<string name="battery_info_health_overheat">"过热"</string>
<string name="battery_info_health_dead">"没电"</string>
<string name="battery_info_health_over_voltage">"过电压"</string>
<string name="battery_info_health_unspecified_failure">"未知错误"</string>
<string name="battery_info_health_cold">"电量低"</string>
<string name="battery_info_power_unplugged">"未插电"</string>
<string name="battery_info_power_ac">"交流电"</string>
<string name="battery_info_power_usb" >"USB"</string>
<string name="battery_info_power_wireless">"无线"</string>
<string name="battery_info_power_ac_usb">"交流电 + USB"</string>
<string name="battery_info_power_unknown">"未知"</string>
<string name="battery_info_status_unknown">"未知"</string>
<string name="battery_info_status_charging">"正在充电"</string>
<string name="battery_info_status_charging_ac">"正在通过交流电源充电"</string>
<string name="battery_info_status_charging_usb">"正在通过USB充电"</string>
<string name="battery_info_status_charging_wireless">"正在进行无线充电"</string>
<string name="battery_info_status_discharging">"未在充电"</string>
<string name="battery_info_status_not_charging">"未在充电"</string>
<string name="battery_info_status_full">"电量充足"</string>
</resources>