android提供的API是BatteryManager类,里面仅提供电量等级与当前电压的参数,并且直接提供了一个电量满的状态码.
下面是一个例子:
显示充电状态的例子
public class MainActivity extends Activity
{
TextView myText;
private BroadcastReceiver mbatteryReceiver=new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action =intent.getAction();
if(Intent.ACTION_BATTERY_CHANGED.equals(action));
{
int status=intent.getIntExtra("status",BatteryManager.BATTERY_STATUS_UNKNOWN);
if(status==BatteryManager.BATTERY_STATUS_CHARGING)
{
myText.setText("充电中");
}
else
{
myText.setText("断电");
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText=(TextView)findViewById(R.id.message);
registerReceiver(mbatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}