Android Power Information view (electricity, temperature, voltage) instance code _android

Source: Internet
Author: User
Tags dateformat stringbuffer

This example describes the Android power information viewing method. Share to everyone for your reference. Specifically 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 your 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.
    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 ch
      Eckbox = (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 the ACTION captured is action_battery_changed, run onbatteryinforeceiver ()/if (Intent.action_battery_chan
        Ged.equals (Action) {sb.append ("\ n current charge:"). Append (Intent.getintextra ("level", 0));
        Sb.append ("\ n battery charge:"). Append (Intent.getintextra ("scale", 100));
        Battery Volt sb.append ("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.bat
          Tery_status_charging:batterystatus = "Charge State";
        Break
          Case BatteryManager.BATTERY_STATUS_DISCHARGING:BatteryStatus = "discharge state";
        Break
          Case BatteryManager.BATTERY_STATUS_NOT_CHARGING:BatteryStatus = "not charged";
        Break
          Case BatteryManager.BATTERY_STATUS_FULL:BatteryStatus = "full power";
        Break
          Case BatteryManager.BATTERY_STATUS_UNKNOWN:BatteryStatus = "not known state";
        Break
        } sb.append ("\ n current status:"). Append (Batterystatus);
        String BatteryStatus2 = null; Switch (Intent.getintextra ("plugged", Batterymanager.battery_plugged_ac)) {case Batterymanager.batter
          Y_plugged_ac:batterystatus2 = "AC charge";
        Break Case BATTERYMANAGER.BATTERY_PLUGGED_USB:BATTERYSTATUS2 = "USB chargerElectricity ";
        Break
        } sb.append ("\ n charge mode:"). Append (BATTERYSTATUS2);
        String batterytemp = null; Switch (Intent.getintextra ("Health", Batterymanager.battery_health_unknown)) {case Batterymanager.bat
          Tery_health_unknown:batterytemp = "Unknown error";
        Break
          Case BatteryManager.BATTERY_HEALTH_GOOD:BatteryTemp = "in good condition";
        Break
          Case BatteryManager.BATTERY_HEALTH_DEAD:BatteryTemp = "battery without electricity";
        Break
          Case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:BatteryTemp = "battery voltage is too high";
        Break
          Case BatteryManager.BATTERY_HEALTH_OVERHEAT:BatteryTemp = "battery overheating";
        Break
        } sb.append ("\ n battery Status:"). Append (batterytemp);
      Log (sb.tostring ());
  }
    }
  };
    Class Spinthread extends Thread {private Boolean mstop;
      public void Quit () {synchronized () {mstop = true; }} public void Run () {while (true) {synchronized (a) {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" Andr oid:layout_height= "Wrap_content" android:layout_marginleft= "25DP" android:layout_margintop= "25DP" Android:text
    Size= "18SP" android:textcolor= "#ffffffff" android:text= "Power Test"/> <scrollview android:id= "@+id/scroll" Android:layout_width= "fill_parent" android:layout_height= "0px" android:layout_weight= "1" > <Text View android:id= "@+id/log" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Andro id: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" @ 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 will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.