android电源信息查看(电量、温度、电压)实例代码_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));
        // 电池伏数
        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
, 查看
电源信息
qt 显示电池电量实例、锂电池电压和电量关系、电池电压与电量的关系、锂电池电压与电量关系、铅酸蓄电池电压与电量,以便于您获取更多的相关知识。

时间: 2024-08-12 02:12:47

android电源信息查看(电量、温度、电压)实例代码_Android的相关文章

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

Android高仿QQ6.0侧滑删除实例代码_Android

推荐阅读: 先给大家分享一下,侧滑删除,布局也就是前面一个item,然后有两个隐藏的按钮(TextView也可以),然后我们可以向左侧滑动,然后显示出来,然后对delete(删除键)实现监听,就可以了哈.好了那就来看看代码怎么实现的吧. 首先和之前一样 自定义View,初始化ViewDragHelper: package com.example.removesidepull; import android.content.Context; import android.support.v4.wi

Android App增量更新详解及实例代码_Android

Android App增量更新实例--Smart App Updates        介绍 你所看到的,是一个用于Android应用程序增量更新的开源库. 包括客户端.服务端两部分代码. 原理 自从 Android 4.1 开始,Google引入了应用程序的增量更新. Link: http://developer.android.com/about/versions/jelly-bean.html Smart app updates is a new feature of Google Pla

Android仿美团分类下拉菜单实例代码_Android

本文实例为大家分享了Android仿美团下拉菜单的实现代码,分类进行选择,供大家参考,具体内容如下 效果图 操作平台 AS2.0 第三方框架:butterknife build.gradle dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile

Android 轻松实现语音识别详解及实例代码_Android

使用Intent调用语音识别程序 说明 Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到语音识别设备,就会抛出异常 ActivityNotFoundException,所以我们需要捕捉这个异常.而且语音识别在模拟器上是无法测试的,因为语音识别是访问google 云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,如果手机不存在语音识别功能的话,也是无法启用识别! 注意:使用前需要安装语音识别程序.如<语音搜索>

Android listview与adapter详解及实例代码_Android

一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常简单. 一个ListView的创建需要3个元素. (1)ListView中的每一列的View. (2)填入View的数据或者图片等. (3)连接数据与ListView的适配器. 也就是说,要使用ListView,首先要了解什么是适配器.适配器是一个连接数据和AdapterView(ListView就

Android 读取assets和raw文件内容实例代码_Android

android之文件操作--读取assets和raw文件下的内容 1.分别创建assets文件夹和res/raw文件夹:(要注意的raw文件是在res下new,然后创建一个名字为raw的文件夹)       2.创建两个txt文件,复制到asset和raw文件夹中: 3.实现的效果: 4.实现代码: (1)布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android

Android PopupWindow全屏详细介绍及实例代码_Android

 Android PopupWindow全屏 很多应用中经常可以看到弹出这种PopupWindow的效果,做了一个小demo分享一下.demo的思路是通过遍历文件,找到图片以及图片文件夹放置在PopupWindow上面.点击按钮可以弹出这个PopupWindow,这里为PopupWindow设置了动画. PopupWindow全屏代码提要 受限需要自定义Popupwindow,这里不看Popupwindow里面要展示的内容,主要是设置Popupwindow的高度. public class Po

Android AsyncTask实现机制详细介绍及实例代码_Android

Android AsyncTask实现机制 示例代码: public final AsyncTask<Params, Progress, Result> execute(Params... params) { return executeOnExecutor(sDefaultExecutor, params); } public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, Pa