監(jiān)聽電池狀態(tài)只需要接收Intent.ACTION_BATTERY_CHANGED的廣播即可,當(dāng)電池狀態(tài)發(fā)生變化時(shí)會(huì)發(fā)出廣播。

1.運(yùn)行狀態(tài)如下圖:
1.充電中的狀態(tài)
2.未充電時(shí)的狀態(tài)
2.實(shí)現(xiàn)代碼如下,各個(gè)狀態(tài)通過名字就很容易知道意思,BatteryManager類中定義了電池狀態(tài)。
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private TextView mTvVoltage;
private TextView mTvTemperature;
private TextView mTvLevel;
private TextView mTvStatus;
private TextView mTvHealth;
private TextView mTvTechnology;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvVoltage = (TextView)findViewById(R.id.tv_voltage);
mTvTemperature = (TextView)findViewById(R.id.tv_temperature);
mTvLevel = (TextView)findViewById(R.id.tv_level);
mTvStatus = (TextView)findViewById(R.id.tv_status);
mTvHealth = (TextView)findViewById(R.id.tv_health);
mTvTechnology = (TextView)findViewById(R.id.tv_technology);
this.registerReceiver(this.mBatteryReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
int voltage=arg1.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
mTvVoltage.setText("電壓:" + voltage / 1000 + "." + voltage % 1000 + "V");
int temperature=arg1.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
mTvTemperature.setText("溫度:" + temperature / 10 + "." + temperature % 10 + "℃");
if (temperature >= 300) {
mTvTemperature.setTextColor(Color.RED);
} else {
mTvTemperature.setTextColor(Color.BLUE);
}
int level=arg1.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
int scale=arg1.getIntExtra(BatteryManager.EXTRA_SCALE,0);
int levelPercent = (int)(((float)level / scale) * 100);
mTvLevel.setText("電量:" + levelPercent + "%");
if (level <= 10) {
mTvLevel.setTextColor(Color.RED);
} else {
mTvLevel.setTextColor(Color.BLUE);
}
int status = arg1.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
String strStatus = "未知狀態(tài)";;
switch (status) {
case BatteryManager.BATTERY_STATUS_CHARGING:
strStatus = "充電中……";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
strStatus = "放電中……";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
strStatus = "未充電";
break;
case BatteryManager.BATTERY_STATUS_FULL:
strStatus = "充電完成";
break;
}
mTvStatus.setText("狀態(tài):" + strStatus);
int health = arg1.getIntExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN);
String strHealth = "未知 :(";;
switch (status) {
case BatteryManager.BATTERY_HEALTH_GOOD:
strHealth = "好 :)";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
strHealth = "過熱!";
break;
case BatteryManager.BATTERY_HEALTH_DEAD: // 未充電時(shí)就會(huì)顯示此狀態(tài),這是什么鬼?
strHealth = "良好";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
strHealth = "電壓過高!";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
strHealth = "未知 :(";
break;
case BatteryManager.BATTERY_HEALTH_COLD:
strHealth = "過冷!";
break;
}
mTvHealth.setText("健康狀況:" + strHealth);
String technology = arg1.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
mTvTechnology.setText("電池技術(shù):" + technology);
}
};
}
本文標(biāo)題:Android電池電量監(jiān)聽的示例代碼-創(chuàng)新互聯(lián)
本文來源:http://chinadenli.net/article18/psjgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、服務(wù)器托管、做網(wǎng)站、定制開發(fā)、品牌網(wǎng)站制作、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容