android電源資訊檢視(電量、溫度、電壓)例項程式碼

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

本文例項講述了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));
// 電池伏數
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 = "充電狀態";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
BatteryStatus = "放電狀態";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
BatteryStatus = "未充電";
break;
case BatteryManager.BATTERY_STATUS_FULL:
BatteryStatus = "充滿電";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
BatteryStatus = "未知道狀態";
break;
}
sb.append("\n當前狀態:").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 = "狀態良好";
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電池狀態:").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中獲取電池電量例項程式碼Android實現偵聽電池狀態顯示、電量及充電動態顯示的方法Android獲取手機電池電量用法例項Android程式設計之電池電量資訊更新的方法(基於BatteryService實現)Android檢視電池電量的方法(基於BroadcastReceiver)Android電池電量跳變Android中檢查、監聽電量和充電狀態的方法解析Android獲取系統cpu資訊,記憶體,版本,電量等資訊的方法詳解Android實現顯示電量的控制元件程式碼Android4.4開發之電池低電量告警提示原理與實現方法分析

相關文章

Android 開發 最新文章