android電源信息查看(電量、溫度、電壓)實例代碼
本文實例講述了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, 就運行onBatteryInfoReceiver()
*/
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
sb.append("\n當前電量:").append(intent.getIntExtra("level", 0));
sb.append("\n電池電量:").append(intent.getIntExtra("scale", 100));
// 電池伏數(shù)
sb.append("\n當前電壓:").append(intent.getIntExtra("voltage", 0));
// 電池溫度
sb.append("\n當前溫度:").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當前狀態(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 = "未知錯誤";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
BatteryTemp = "狀態(tài)良好";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
BatteryTemp = "電池沒有電";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
BatteryTemp = "電池電壓過高";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
BatteryTemp = "電池過熱";
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="電源測試"
/>
<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>
希望本文所述對大家的Android程序設計有所幫助。
相關文章
Android程序開發(fā)之ListView 與PopupWindow實現(xiàn)從左向右滑動刪除功能
這篇文章主要介紹了Android程序開發(fā)之ListView 與PopupWindow實現(xiàn)滑動刪除功能的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
Kotlin基礎學習之Deprecated與Suppress注解使用
這篇文章主要給大家介紹了關于Kotlin基礎學習之Deprecated與Suppress注解使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Kotlin具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-08-08
Android 手機瀏覽器調試使用Chrome進行調試實例詳解
這篇文章主要介紹了Android 手機瀏覽器調試使用Chrome進行調試實例詳解的相關資料,這里提供了實例,需要的朋友可以參考下2016-12-12
Android中通過反射實現(xiàn)圓角ImageView代碼實例
這篇文章主要介紹了Android中通過反射實現(xiàn)圓角ImageView代碼實例,本文直接給出核心實現(xiàn)代碼,需要的朋友可以參考下2015-04-04
探究Android客戶端網(wǎng)絡預連接優(yōu)化機制
一般情況下,我們都是用一些封裝好的網(wǎng)絡框架去請求網(wǎng)絡,對底層實現(xiàn)不甚關注,而大部分情況下也不需要特別關注處理。了解底層的一些實現(xiàn),有益于我們對網(wǎng)絡加載進行優(yōu)化。本文就是關于根據(jù)http的連接復用機制來優(yōu)化網(wǎng)絡加載速度的原理與細節(jié)2021-06-06
Android開發(fā)實現(xiàn)Gallery畫廊效果的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)Gallery畫廊效果的方法,結合具體實例形式分析了Android使用Gallery實現(xiàn)畫廊功能的具體操作技巧與相關注意事項,需要的朋友可以參考下2017-06-06
Android 使用fast-verification實現(xiàn)驗證碼填寫功能的實例代碼
這篇文章主要介紹了Android 使用fast-verification實現(xiàn)驗證碼填寫功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android開發(fā)之資源目錄assets與res/raw的區(qū)別分析
這篇文章主要介紹了Android開發(fā)之資源目錄assets與res/raw的區(qū)別,結合實例形式分析了Android開發(fā)中資源目錄assets與res/raw的具體功能、使用方法與區(qū)別,需要的朋友可以參考下2016-01-01

