返回顶部
首页 > 资讯 > 移动开发 >android电源信息查看(电量、温度、电压)实例代码
  • 359
分享到

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

Android 2022-06-06 09:06:45 359人浏览 薄情痞子
摘要

本文实例讲述了Android电源信息查看方法。分享给大家供大家参考。具体如下: 1. PowerTestActivity: import android.app.Activi

本文实例讲述了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;

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();
      
      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开发之电池低电量告警提示原理与实现方法分析


--结束END--

本文标题: android电源信息查看(电量、温度、电压)实例代码

本文链接: https://lsjlt.com/news/26497.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作