Home>

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 the captured action is action_battery_changed, run onbatteryinforeceiver ()
       * /
      if (intent.action_battery_changed.equals (action)) {
        sb.append ("\ nCurrent battery:"). append (intent.getintextra ("level", 0));
        sb.append ("\ nBattery level:"). append (intent.getintextra ("scale", 100));
        //battery voltage
        sb.append ("\ nCurrent voltage:"). append (intent.getintextra ("voltage", 0));
        //battery temperature
        sb.append ("\ nCurrent 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 status";
          break;
        case batterymanager.battery_status_not_charging:
          batterystatus="not charged";
          break;
        case batterymanager.battery_status_full:
          batterystatus="Fully charged";
          break;
        case batterymanager.battery_status_unknown:
          batterystatus="Unknown status";
          break;
        }
        sb.append ("\ nCurrent 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 ("\ nCharging method:"). append (batterystatus2);
        string batterytemp=null;
        switch (intent.getintextra ("health",            batterymanager.battery_health_unknown)) {
        case batterymanager.battery_health_unknown:
          batterytemp="Unknown Error";
          break;
        case batterymanager.battery_health_good:
          batterytemp="Good condition";
          break;
        case batterymanager.battery_health_dead:
          batterytemp="Battery is dead";
          break;
        case batterymanager.battery_health_over_voltage:
          batterytemp="Battery voltage is too high";
          break;
        case batterymanager.battery_health_overheat:
          batterytemp="Battery is too hot";
          break;
        }
        sb.append ("\ nBattery 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="Power 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>
  • Previous Talking about the Destructor of C ++ Base Class as Virtual Function
  • Next Implementation of strcpy function in C ++