android Power information View of power temperature voltage instance code

  • 2020-08-22 22:39:33
  • OfStack

This article describes the android power information view method. Share to everybody for everybody reference. The details are as follows:

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();
      /*
       *  If it's caught action is ACTION_BATTERY_CHANGED .   Just run onBatteryInfoReceiver()
       */
      if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
        sb.append("\n The current power :").append(intent.getIntExtra("level", 0));
        sb.append("\n battery :").append(intent.getIntExtra("scale", 100));
        //  Battery voltage 
        sb.append("\n Current voltage: ").append(intent.getIntExtra("voltage", 0));
        //  Battery temperature 
        sb.append("\n Current temperature: ").append(
            intent.getIntExtra("temperature", 0));
        String BatteryStatus = null;
        switch (intent.getIntExtra("status",
            BatteryManager.BATTERY_STATUS_UNKNOWN)) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
          BatteryStatus = " Charging status ";
          break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
          BatteryStatus = " discharge ";
          break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
          BatteryStatus = " Not charging ";
          break;
        case BatteryManager.BATTERY_STATUS_FULL:
          BatteryStatus = " A full charge ";
          break;
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
          BatteryStatus = " Unknown state ";
          break;
        }
        sb.append("\n Current status: ").append(BatteryStatus);
        String BatteryStatus2 = null;
        switch (intent.getIntExtra("plugged",
            BatteryManager.BATTERY_PLUGGED_AC)) {
        case BatteryManager.BATTERY_PLUGGED_AC:
          BatteryStatus2 = "AC charging ";
          break;
        case BatteryManager.BATTERY_PLUGGED_USB:
          BatteryStatus2 = "USB charging ";
          break;
        }
        sb.append("\n Charging mode: ").append(BatteryStatus2);
        String BatteryTemp = null;
        switch (intent.getIntExtra("health",
            BatteryManager.BATTERY_HEALTH_UNKNOWN)) {
        case BatteryManager.BATTERY_HEALTH_UNKNOWN:
          BatteryTemp = " An unknown error ";
          break;
        case BatteryManager.BATTERY_HEALTH_GOOD:
          BatteryTemp = " In good condition ";
          break;
        case BatteryManager.BATTERY_HEALTH_DEAD:
          BatteryTemp = " The battery is dead ";
          break;
        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
          BatteryTemp = " High battery voltage ";
          break;
        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
          BatteryTemp = " Battery overheat ";
          break;
        }
        sb.append("\n Battery status: ").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=" The power supply test "
    />
  <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>

I hope this article has been helpful for your Android programming.


Related articles: