欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android电源信息查看(电量、温度、电压)实例代码

程序员文章站 2023-11-29 22:26:22
本文实例讲述了android电源信息查看方法。分享给大家供大家参考。具体如下: 1. powertestactivity: import android.app...

本文实例讲述了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程序设计有所帮助。