欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Andriod 獲取電池的信息實例代碼

 更新時間:2016年03月01日 10:37:11   作者:gisoracle  
通過本段實例代碼給大家介紹Andriod 獲取電池的信息的相關(guān)知識,對android獲取電池信息相關(guān)知識感興趣的朋友一起學習吧

具體代碼如下所示:

<?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);
// 得到布局中的所有對象
findView();
// 設(shè)置對象的監(jiān)聽器
setListener();
}
private void findView() {
// 得到布局中的所有對象
btnBattery = (Button) findViewById(R.id.btn_battery);
tvBattery = (TextView) findViewById(R.id.tv_battery);
}
// 設(shè)置對象的監(jiān)聽器
private void setListener() {
btnBattery.setOnClickListener(listener);
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
// 當前的音量
case R.id.btn_battery:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBroadcastReceiver, filter);
break;
}
}
};
// 聲明廣播接受者對象
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:電池沒有電。
// BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:電池電壓過高。
// BatteryManager.BATTERY_HEALTH_OVERHEAT:電池過熱。
// BatteryManager.BATTERY_HEALTH_UNKNOWN:未知狀態(tài)。
int health = intent.getIntExtra("health", 0);
// boolean類型
boolean present = intent.getBooleanExtra("present", false);
// 得到電池剩余容量
int level = intent.getIntExtra("level", 0);
// 得到電池最大值。通常為100。
int scale = intent.getIntExtra("scale", 0);
// 得到圖標ID
int icon_small = intent.getIntExtra("icon-small", 0);
// 充電方式: BatteryManager.BATTERY_PLUGGED_AC:AC充電。 BatteryManager.BATTERY_PLUGGED_USB:USB充電。
int plugged = intent.getIntExtra("plugged", 0);
// 得到電池的電壓
int voltage = intent.getIntExtra("voltage", 0);
// 得到電池的溫度,0.1度單位。例如 表示197的時候,意思為19.7度
int temperature = intent.getIntExtra("temperature", 0);
// 得到電池的類型
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小圖標:" + icon_small
+ "\n充電方式:" + plugged
+ "\n充電方式: " + acString
+ "\n電池的電壓:" + voltage
+ "\n電池的溫度:" + (float) temperature * 0.1
+ "\n電池的類型:" + technology);
}
}
};
@Override
protected void onPause() {
super.onPause();
// 解除注冊監(jiān)聽
unregisterReceiver(mBroadcastReceiver);
}
}

以上所述是小編給大家介紹的Andriod 獲取電池的信息實例代碼,希望對大家有所幫助!

相關(guān)文章

  • Android中WebView用法實例分析

    Android中WebView用法實例分析

    這篇文章主要介紹了Android中WebView用法,以實例形式較為詳細的分析了Android中WebView的功能、注意事項與使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-10-10
  • Android中實現(xiàn)圓角圖片的幾種方法

    Android中實現(xiàn)圓角圖片的幾種方法

    本篇文章主要介紹了Android中實現(xiàn)圓角圖片的幾種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 浮動AppBar中的textField焦點回滾問題解決

    浮動AppBar中的textField焦點回滾問題解決

    這篇文章主要為大家介紹了浮動AppBar中的textField焦點回滾問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Android中關(guān)于CoordinatorLayout的一些實用布局技巧

    Android中關(guān)于CoordinatorLayout的一些實用布局技巧

    大家都知道CoordinatorLayout是一個“加強版”的 FrameLayout,那么下面這篇文章主要給大家分享了Android中關(guān)于CoordinatorLayout的一些布局技巧,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • Android仿淘寶首頁頭條View垂直滾動效果

    Android仿淘寶首頁頭條View垂直滾動效果

    這篇文章主要為大家詳細介紹了Android仿淘寶首頁頭條View垂直滾動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android Studio中Logcat寫入和查看日志

    Android Studio中Logcat寫入和查看日志

    大家好,本篇文章主要講的是Android Studio中Logcat寫入和查看日志,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Android app開發(fā)中Retrofit框架的初步上手使用

    Android app開發(fā)中Retrofit框架的初步上手使用

    這篇文章主要介紹了Android app開發(fā)中Retrofit框架的初步上手使用,Retrofit 2.0發(fā)布以來獲得了巨大的人氣增長,并且經(jīng)常被開發(fā)者們拿來與Volley比較,需要的朋友可以參考下
    2016-02-02
  • 解析android中系統(tǒng)日期時間的獲取

    解析android中系統(tǒng)日期時間的獲取

    本篇文章是對在android中,如何系統(tǒng)日期時間獲取的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • 使用ViewPager實現(xiàn)高仿launcher左右拖動效果

    使用ViewPager實現(xiàn)高仿launcher左右拖動效果

    今天用ViewPager這個類實現(xiàn)了同樣的左右拖動效果,這樣代碼更少,但是效果是一樣的,ViewPager是實現(xiàn)左右兩個屏幕平滑地切換的一個類,它是Google提供的,有需要的朋友可以了解下
    2013-01-01
  • Flutter?web?bridge?通信總結(jié)分析詳解

    Flutter?web?bridge?通信總結(jié)分析詳解

    這篇文章主要為大家介紹了Flutter?web?bridge?通信總結(jié)分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01

最新評論