Andriod 獲取電池的信息實(shí)例代碼
具體代碼如下所示:
<?xml version="1.0"?>
<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="獲取電池的信息" android:id="@+id/btn_battery"/>
<TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tv_battery"/>
</LinearLayout>
package com.example.yanlei.wifi;
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.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// 定義電池信息的按鈕
private Button btnBattery;
// 定義顯示電池信息的textview
private TextView tvBattery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 得到布局中的所有對(duì)象
findView();
// 設(shè)置對(duì)象的監(jiān)聽(tīng)器
setListener();
}
private void findView() {
// 得到布局中的所有對(duì)象
btnBattery = (Button) findViewById(R.id.btn_battery);
tvBattery = (TextView) findViewById(R.id.tv_battery);
}
// 設(shè)置對(duì)象的監(jiān)聽(tīng)器
private void setListener() {
btnBattery.setOnClickListener(listener);
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
// 當(dāng)前的音量
case R.id.btn_battery:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBroadcastReceiver, filter);
break;
}
}
};
// 聲明廣播接受者對(duì)象
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
// 得到電池狀態(tài):
// BatteryManager.BATTERY_STATUS_CHARGING:充電狀態(tài)。
// BatteryManager.BATTERY_STATUS_DISCHARGING:放電狀態(tài)。
// BatteryManager.BATTERY_STATUS_NOT_CHARGING:未充滿。
// BatteryManager.BATTERY_STATUS_FULL:充滿電。
// BatteryManager.BATTERY_STATUS_UNKNOWN:未知狀態(tài)。
int status = intent.getIntExtra("status", 0);
// 得到健康狀態(tài):
// BatteryManager.BATTERY_HEALTH_GOOD:狀態(tài)良好。
// BatteryManager.BATTERY_HEALTH_DEAD:電池沒(méi)有電。
// BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:電池電壓過(guò)高。
// BatteryManager.BATTERY_HEALTH_OVERHEAT:電池過(guò)熱。
// BatteryManager.BATTERY_HEALTH_UNKNOWN:未知狀態(tài)。
int health = intent.getIntExtra("health", 0);
// boolean類(lèi)型
boolean present = intent.getBooleanExtra("present", false);
// 得到電池剩余容量
int level = intent.getIntExtra("level", 0);
// 得到電池最大值。通常為100。
int scale = intent.getIntExtra("scale", 0);
// 得到圖標(biāo)ID
int icon_small = intent.getIntExtra("icon-small", 0);
// 充電方式: BatteryManager.BATTERY_PLUGGED_AC:AC充電?!atteryManager.BATTERY_PLUGGED_USB:USB充電。
int plugged = intent.getIntExtra("plugged", 0);
// 得到電池的電壓
int voltage = intent.getIntExtra("voltage", 0);
// 得到電池的溫度,0.1度單位。例如 表示197的時(shí)候,意思為19.7度
int temperature = intent.getIntExtra("temperature", 0);
// 得到電池的類(lèi)型
String technology = intent.getStringExtra("technology");
// 得到電池狀態(tài)
String statusString = "";
// 根據(jù)狀態(tài)id,得到狀態(tài)字符串
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
statusString = "unknown";
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
statusString = "charging";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
statusString = "discharging";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
statusString = "not charging";
break;
case BatteryManager.BATTERY_STATUS_FULL:
statusString = "full";
break;
}
//得到電池的壽命狀態(tài)
String healthString = "";
//根據(jù)狀態(tài)id,得到電池壽命
switch (health) {
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
healthString = "unknown";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
healthString = "good";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
healthString = "overheat";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
healthString = "dead";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
healthString = "voltage";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
healthString = "unspecified failure";
break;
}
//得到充電模式
String acString = "";
//根據(jù)充電狀態(tài)id,得到充電模式
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
acString = "plugged ac";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
acString = "plugged usb";
break;
}
//顯示電池信息
tvBattery.setText("電池的狀態(tài):" + statusString
+ "\n健康值: "+ healthString
+ "\n電池剩余容量: " + level
+ "\n電池的最大值:" + scale
+ "\n小圖標(biāo):" + icon_small
+ "\n充電方式:" + plugged
+ "\n充電方式: " + acString
+ "\n電池的電壓:" + voltage
+ "\n電池的溫度:" + (float) temperature * 0.1
+ "\n電池的類(lèi)型:" + technology);
}
}
};
@Override
protected void onPause() {
super.onPause();
// 解除注冊(cè)監(jiān)聽(tīng)
unregisterReceiver(mBroadcastReceiver);
}
}
以上所述是小編給大家介紹的Andriod 獲取電池的信息實(shí)例代碼,希望對(duì)大家有所幫助!
- Andriod 自定義控件之音頻條
- Android學(xué)習(xí)教程之滑動(dòng)布局(14)
- Android開(kāi)發(fā)中常見(jiàn)問(wèn)題
- Java實(shí)現(xiàn)Andriod帶看括弧的計(jì)算器代碼
- Andriod arcgis保存Mapview為圖片的實(shí)例代碼
- Andriod 資源文件之存取操作
- Andriod 讀取網(wǎng)絡(luò)圖片實(shí)例代碼解析
- Andriod開(kāi)發(fā)中引入jar包的正確方式介紹
- 代碼從windows下visual studio到andriod平臺(tái)遷移實(shí)現(xiàn)步驟
相關(guān)文章
Android實(shí)現(xiàn)動(dòng)態(tài)體溫計(jì)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)體溫計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
淺談關(guān)于Android路由的實(shí)現(xiàn)
本篇文章主要介紹了淺談關(guān)于Android路由的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Android實(shí)現(xiàn)外部喚起應(yīng)用跳轉(zhuǎn)指定頁(yè)面的方法
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)外部喚起應(yīng)用跳轉(zhuǎn)指定頁(yè)面的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
關(guān)于Fragment?already?added問(wèn)題的解決方案
這篇文章主要介紹了關(guān)于Fragment?already?added問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Android程序報(bào)錯(cuò)程序包org.apache.http不存在問(wèn)題的解決方法
這篇文章主要介紹了Android程序報(bào)錯(cuò)"程序包org.apache.http不存在——Android 6.0已經(jīng)不支持HttpClient" 問(wèn)題的解決方法,感興趣的小伙伴們可以參考一下2016-06-06
Glide實(shí)現(xiàn)加載圖片顯示進(jìn)度條效果
Glide作為安卓開(kāi)發(fā)常用的圖片加載庫(kù),有許多實(shí)用而且強(qiáng)大的功能,那么,下面這篇文章主要給大家介紹了利用Glide實(shí)現(xiàn)加載圖片顯示進(jìn)度條效果的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下來(lái)一起看看吧。2017-05-05
android動(dòng)態(tài)設(shè)置app當(dāng)前運(yùn)行語(yǔ)言的方法
下面小編就為大家?guī)?lái)一篇android動(dòng)態(tài)設(shè)置app當(dāng)前運(yùn)行語(yǔ)言的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
View Controller Transition實(shí)現(xiàn)京東加購(gòu)物車(chē)效果
這篇文章主要介紹了View Controller Transition實(shí)現(xiàn)京東加購(gòu)物車(chē)效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
三行Android代碼實(shí)現(xiàn)白天夜間模式流暢切換
這篇文章主要為大家分享了三行Android代碼實(shí)現(xiàn)白天夜間模式流暢切換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

