Android电源信息

[代码] PowerTestActivity

view source
print ?
001 import android.app.Activity;
002 import android.content.BroadcastReceiver;
003 import android.content.Context;
004 import android.content.Intent;
005 import android.content.IntentFilter;
006 import android.os.BatteryManager;
007 import android.os.Bundle;
008 import android.os.PowerManager;
009 import android.view.View;
010 import android.widget.CheckBox;
011 import android.widget.TextView;
012   
013 import java.text.DateFormat;
014 import java.util.Date;
015   
016 /**
017  * So you thought sync used up your battery life.
018  */
019 public class PowerTestActivity extends Activity {
020     TextView mLog;
021     DateFormat mDateFormat;
022     IntentFilter mFilter;
023     PowerManager.WakeLock mWakeLock;
024     SpinThread mThread;
025   
026     @Override
027     public void onCreate(Bundle savedInstanceState) {
028         super.onCreate(savedInstanceState);
029   
030         // Set the layout for this activity.  You can find it
031         // in res/layout/hello_activity.xml
032         setContentView(R.layout.main);
033   
034         findViewById(R.id.checkbox).setOnClickListener(mClickListener);
035         mLog = (TextView)findViewById(R.id.log);
036   
037         mDateFormat = DateFormat.getInstance();
038   
039         mFilter = new IntentFilter();
040         mFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
041         mFilter.addAction(Intent.ACTION_BATTERY_LOW);
042         mFilter.addAction(Intent.ACTION_BATTERY_OKAY);
043         mFilter.addAction(Intent.ACTION_POWER_CONNECTED);
044   
045         PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
046         mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "BatteryWaster");
047         mWakeLock.setReferenceCounted(false);
048     }
049   
050     @Override
051     public void onPause() {
052         stopRunning();
053     }
054   
055     View.OnClickListener mClickListener = new View.OnClickListener() {
056         public void onClick(View v) {
057             CheckBox checkbox = (CheckBox)v;
058             if (checkbox.isChecked()) {
059                 startRunning();
060             } else {
061                 stopRunning();
062             }
063         }
064     };
065   
066     void startRunning() {
067         log("Start");
068         registerReceiver(mReceiver, mFilter);
069         mWakeLock.acquire();
070         if (mThread == null) {
071             mThread = new SpinThread();
072             mThread.start();
073         }
074     }
075   
076     void stopRunning() {
077         log("Stop");
078         unregisterReceiver(mReceiver);
079         mWakeLock.release();
080         if (mThread != null) {
081             mThread.quit();
082             mThread = null;
083         }
084     }
085   
086     void log(String s) {
087         mLog.setText(mLog.getText() + "\n" + mDateFormat.format(new Date()) + ": " + s);
088     }
089   
090     BroadcastReceiver mReceiver = new BroadcastReceiver() {
091         public void onReceive(Context context, Intent intent) {
092             StringBuffer sb = new StringBuffer();
093             String action = intent.getAction();
094             /*
095              * 如果捕捉到的action是ACTION_BATTERY_CHANGED, 就运行onBatteryInfoReceiver()
096              */
097             if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
098   
099                 sb.append("\n当前电量:").append(intent.getIntExtra("level", 0));
100                 sb.append("\n电池电量:").append(intent.getIntExtra("scale", 100));
101                 // 电池伏数
102                 sb.append("\n当前电压:").append(intent.getIntExtra("voltage", 0));
103                 // 电池温度
104                 sb.append("\n当前温度:").append(
105                         intent.getIntExtra("temperature", 0));
106                 String BatteryStatus = null;
107                 switch (intent.getIntExtra("status",
108                         BatteryManager.BATTERY_STATUS_UNKNOWN)) {
109                 case BatteryManager.BATTERY_STATUS_CHARGING:
110                     BatteryStatus = "充电状态";
111                     break;
112                 case BatteryManager.BATTERY_STATUS_DISCHARGING:
113                     BatteryStatus = "放电状态";
114                     break;
115                 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
116                     BatteryStatus = "未充电";
117                     break;
118                 case BatteryManager.BATTERY_STATUS_FULL:
119                     BatteryStatus = "充满电";
120                     break;
121                 case BatteryManager.BATTERY_STATUS_UNKNOWN:
122                     BatteryStatus = "未知道状态";
123                     break;
124                 }
125                 sb.append("\n当前状态:").append(BatteryStatus);
126                 String BatteryStatus2 = null;
127                 switch (intent.getIntExtra("plugged",
128                         BatteryManager.BATTERY_PLUGGED_AC)) {
129                 case BatteryManager.BATTERY_PLUGGED_AC:
130                     BatteryStatus2 = "AC充电";
131                     break;
132                 case BatteryManager.BATTERY_PLUGGED_USB:
133                     BatteryStatus2 = "USB充电";
134                     break;
135                 }
136                 sb.append("\n充电方式:").append(BatteryStatus2);
137                 String BatteryTemp = null;
138                 switch (intent.getIntExtra("health",
139                         BatteryManager.BATTERY_HEALTH_UNKNOWN)) {
140                 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
141                     BatteryTemp = "未知错误";
142                     break;
143                 case BatteryManager.BATTERY_HEALTH_GOOD:
144                     BatteryTemp = "状态良好";
145                     break;
146                 case BatteryManager.BATTERY_HEALTH_DEAD:
147                     BatteryTemp = "电池没有电";
148                     break;
149                 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
150                     BatteryTemp = "电池电压过高";
151                     break;
152                 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
153                     BatteryTemp = "电池过热";
154                     break;
155                 }
156                 sb.append("\n电池状态:").append(BatteryTemp);
157                 log(sb.toString());
158             }
159         }
160     };
161   
162     class SpinThread extends Thread {
163         private boolean mStop;
164   
165         public void quit() {
166             synchronized (this) {
167                 mStop = true;
168             }
169         }
170   
171         public void run() {
172             while (true) {
173                 synchronized (this) {
174                     if (mStop) {
175                         return;
176                     }
177                 }
178             }
179         }
180     }
181 }

[代码] main.xml

view source
print ?
01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent"
06     >
07 <CheckBox android:id="@+id/checkbox"
08         android:layout_width="fill_parent"
09         android:layout_height="wrap_content"
10         android:layout_marginLeft="25dp"
11         android:layout_marginTop="25dp"
12         android:textSize="18sp"
13         android:textColor="#ffffffff"
14         android:text="电源测试"
15         />
16   
17     <ScrollView android:id="@+id/scroll"
18         android:layout_width="fill_parent"
19         android:layout_height="0px"
20         android:layout_weight="1"
21         >
22         <TextView android:id="@+id/log"
23             android:layout_width="fill_parent"
24             android:layout_height="wrap_content"
25             android:layout_marginTop="25dp"
26             android:textSize="12sp"
27             android:textColor="#ffffffff"
28             />
29     </ScrollView>
30 </LinearLayout>

[代码] AndroidManifest.xml

view source
print ?
01 <?xml version="1.0" encoding="utf-8"?>
02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
03       package="com.lenovo.android"
04       android:versionCode="1"
05       android:versionName="1.0">
06     <uses-sdk android:minSdkVersion="8" />
07   
08     <application android:icon="@drawable/icon" android:label="@string/app_name">
09         <activity android:name=".PowerTestActivity"
10                   android:label="@string/app_name">
11             <intent-filter>
12                 <action android:name="android.intent.action.MAIN" />
13                 <category android:name="android.intent.category.LAUNCHER" />
14             </intent-filter>
15         </activity>
16           
17     </application>
18     <uses-permission android:name="android.permission.WAKE_LOCK" />
19     <uses-permission android:name="android.permission.DEVICE_POWER" />
20 </manifest>

你可能感兴趣的:(android,String,layout,null,action,import)