android電源信息查看(電量、溫度、電壓)實(shí)例代碼
本文實(shí)例講述了android電源信息查看方法。分享給大家供大家參考。具體如下:
1. PowerTestActivity:
import android.app.Activity;
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.os.PowerManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.Date;
/**
* So you thought sync used up your battery life.
*/
public class PowerTestActivity extends Activity {
TextView mLog;
DateFormat mDateFormat;
IntentFilter mFilter;
PowerManager.WakeLock mWakeLock;
SpinThread mThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the layout for this activity. You can find it
// in res/layout/hello_activity.xml
setContentView(R.layout.main);
findViewById(R.id.checkbox).setOnClickListener(mClickListener);
mLog = (TextView)findViewById(R.id.log);
mDateFormat = DateFormat.getInstance();
mFilter = new IntentFilter();
mFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
mFilter.addAction(Intent.ACTION_BATTERY_LOW);
mFilter.addAction(Intent.ACTION_BATTERY_OKAY);
mFilter.addAction(Intent.ACTION_POWER_CONNECTED);
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "BatteryWaster");
mWakeLock.setReferenceCounted(false);
}
@Override
public void onPause() {
stopRunning();
}
View.OnClickListener mClickListener = new View.OnClickListener() {
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v;
if (checkbox.isChecked()) {
startRunning();
} else {
stopRunning();
}
}
};
void startRunning() {
log("Start");
registerReceiver(mReceiver, mFilter);
mWakeLock.acquire();
if (mThread == null) {
mThread = new SpinThread();
mThread.start();
}
}
void stopRunning() {
log("Stop");
unregisterReceiver(mReceiver);
mWakeLock.release();
if (mThread != null) {
mThread.quit();
mThread = null;
}
}
void log(String s) {
mLog.setText(mLog.getText() + "\n" + mDateFormat.format(new Date()) + ": " + s);
}
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
StringBuffer sb = new StringBuffer();
String action = intent.getAction();
/*
* 如果捕捉到的action是ACTION_BATTERY_CHANGED, 就運(yùn)行onBatteryInfoReceiver()
*/
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
sb.append("\n當(dāng)前電量:").append(intent.getIntExtra("level", 0));
sb.append("\n電池電量:").append(intent.getIntExtra("scale", 100));
// 電池伏數(shù)
sb.append("\n當(dāng)前電壓:").append(intent.getIntExtra("voltage", 0));
// 電池溫度
sb.append("\n當(dāng)前溫度:").append(
intent.getIntExtra("temperature", 0));
String BatteryStatus = null;
switch (intent.getIntExtra("status",
BatteryManager.BATTERY_STATUS_UNKNOWN)) {
case BatteryManager.BATTERY_STATUS_CHARGING:
BatteryStatus = "充電狀態(tài)";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
BatteryStatus = "放電狀態(tài)";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
BatteryStatus = "未充電";
break;
case BatteryManager.BATTERY_STATUS_FULL:
BatteryStatus = "充滿電";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
BatteryStatus = "未知道狀態(tài)";
break;
}
sb.append("\n當(dāng)前狀態(tài):").append(BatteryStatus);
String BatteryStatus2 = null;
switch (intent.getIntExtra("plugged",
BatteryManager.BATTERY_PLUGGED_AC)) {
case BatteryManager.BATTERY_PLUGGED_AC:
BatteryStatus2 = "AC充電";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
BatteryStatus2 = "USB充電";
break;
}
sb.append("\n充電方式:").append(BatteryStatus2);
String BatteryTemp = null;
switch (intent.getIntExtra("health",
BatteryManager.BATTERY_HEALTH_UNKNOWN)) {
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
BatteryTemp = "未知錯(cuò)誤";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
BatteryTemp = "狀態(tài)良好";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
BatteryTemp = "電池沒(méi)有電";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
BatteryTemp = "電池電壓過(guò)高";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
BatteryTemp = "電池過(guò)熱";
break;
}
sb.append("\n電池狀態(tài):").append(BatteryTemp);
log(sb.toString());
}
}
};
class SpinThread extends Thread {
private boolean mStop;
public void quit() {
synchronized (this) {
mStop = true;
}
}
public void run() {
while (true) {
synchronized (this) {
if (mStop) {
return;
}
}
}
}
}
}
2. main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox android:id="@+id/checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="25dp"
android:textSize="18sp"
android:textColor="#ffffffff"
android:text="電源測(cè)試"
/>
<ScrollView android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
>
<TextView android:id="@+id/log"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textSize="12sp"
android:textColor="#ffffffff"
/>
</ScrollView>
</LinearLayout>
3. AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lenovo.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PowerTestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DEVICE_POWER" />
</manifest>
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- Android中檢查、監(jiān)聽電量和充電狀態(tài)的方法
- 解析Android獲取系統(tǒng)cpu信息,內(nèi)存,版本,電量等信息的方法詳解
- Android實(shí)現(xiàn)偵聽電池狀態(tài)顯示、電量及充電動(dòng)態(tài)顯示的方法
- Android中獲取電池電量實(shí)例代碼
- Android獲取手機(jī)電池電量用法實(shí)例
- Android編程之電池電量信息更新的方法(基于BatteryService實(shí)現(xiàn))
- Android實(shí)現(xiàn)顯示電量的控件代碼
- Android電池電量監(jiān)聽的示例代碼
- 獲取Android設(shè)備電池電量狀態(tài)
相關(guān)文章
務(wù)必掌握的Android十六進(jìn)制狀態(tài)管理最佳實(shí)踐
這篇文章主要為大家介紹了務(wù)必掌握的Android十六進(jìn)制狀態(tài)管理最佳實(shí)踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
詳解Android studio 3+版本apk安裝失敗問(wèn)題
這篇文章主要介紹了詳解Android studio 3+版本apk安裝失敗問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Android實(shí)戰(zhàn)打飛機(jī)游戲之無(wú)限循環(huán)的背景圖(2)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)打飛機(jī)游戲之無(wú)限循環(huán)的背景圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android Studio啟動(dòng)報(bào)錯(cuò)Java 1.8 or later is required的解決方法
這篇文章主要為大家詳細(xì)介紹了Android Studio啟動(dòng)時(shí)報(bào)錯(cuò)Java 1.8 or later is required的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android 如何攔截用戶頻繁操作(點(diǎn)擊事件)
本文主要介紹了Android 如何攔截用戶頻繁操作,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Android點(diǎn)擊Button實(shí)現(xiàn)功能的幾種方法總結(jié)
當(dāng)Button有多個(gè)或者Button的使用次數(shù)很多時(shí),我們需要采用綁定監(jiān)聽器的做法,其實(shí),綁定監(jiān)聽器也有幾種方法,不過(guò),我在這里就不一一列舉了,畢竟那些方法在實(shí)際的應(yīng)用中也不常見2013-10-10
Android屏幕手勢(shì)檢測(cè)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android屏幕手勢(shì)檢測(cè)的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
Eclipse+ADT+Android SDK搭建安卓開發(fā)環(huán)境的實(shí)現(xiàn)步驟
這篇文章主要介紹了Eclipse+ADT+Android SDK搭建安卓開發(fā)環(huán)境的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

