android电源信息查看(电量、温度、电压)实例代码
本文实例讲述了android电源信息查看方法。分享给大家供大家参考。具体如下:
1.PowerTestActivity:
importandroid.app.Activity; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.os.BatteryManager; importandroid.os.Bundle; importandroid.os.PowerManager; importandroid.view.View; importandroid.widget.CheckBox; importandroid.widget.TextView; importjava.text.DateFormat; importjava.util.Date; /** *Soyouthoughtsyncusedupyourbatterylife. */ publicclassPowerTestActivityextendsActivity{ TextViewmLog; DateFormatmDateFormat; IntentFiltermFilter; PowerManager.WakeLockmWakeLock; SpinThreadmThread; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //Setthelayoutforthisactivity.Youcanfindit //inres/layout/hello_activity.xml setContentView(R.layout.main); findViewById(R.id.checkbox).setOnClickListener(mClickListener); mLog=(TextView)findViewById(R.id.log); mDateFormat=DateFormat.getInstance(); mFilter=newIntentFilter(); mFilter.addAction(Intent.ACTION_BATTERY_CHANGED); mFilter.addAction(Intent.ACTION_BATTERY_LOW); mFilter.addAction(Intent.ACTION_BATTERY_OKAY); mFilter.addAction(Intent.ACTION_POWER_CONNECTED); PowerManagerpm=(PowerManager)getSystemService(POWER_SERVICE); mWakeLock=pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,"BatteryWaster"); mWakeLock.setReferenceCounted(false); } @Override publicvoidonPause(){ stopRunning(); } View.OnClickListenermClickListener=newView.OnClickListener(){ publicvoidonClick(Viewv){ CheckBoxcheckbox=(CheckBox)v; if(checkbox.isChecked()){ startRunning(); }else{ stopRunning(); } } }; voidstartRunning(){ log("Start"); registerReceiver(mReceiver,mFilter); mWakeLock.acquire(); if(mThread==null){ mThread=newSpinThread(); mThread.start(); } } voidstopRunning(){ log("Stop"); unregisterReceiver(mReceiver); mWakeLock.release(); if(mThread!=null){ mThread.quit(); mThread=null; } } voidlog(Strings){ mLog.setText(mLog.getText()+"\n"+mDateFormat.format(newDate())+":"+s); } BroadcastReceivermReceiver=newBroadcastReceiver(){ publicvoidonReceive(Contextcontext,Intentintent){ StringBuffersb=newStringBuffer(); Stringaction=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)); StringBatteryStatus=null; switch(intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN)){ caseBatteryManager.BATTERY_STATUS_CHARGING: BatteryStatus="充电状态"; break; caseBatteryManager.BATTERY_STATUS_DISCHARGING: BatteryStatus="放电状态"; break; caseBatteryManager.BATTERY_STATUS_NOT_CHARGING: BatteryStatus="未充电"; break; caseBatteryManager.BATTERY_STATUS_FULL: BatteryStatus="充满电"; break; caseBatteryManager.BATTERY_STATUS_UNKNOWN: BatteryStatus="未知道状态"; break; } sb.append("\n当前状态:").append(BatteryStatus); StringBatteryStatus2=null; switch(intent.getIntExtra("plugged", BatteryManager.BATTERY_PLUGGED_AC)){ caseBatteryManager.BATTERY_PLUGGED_AC: BatteryStatus2="AC充电"; break; caseBatteryManager.BATTERY_PLUGGED_USB: BatteryStatus2="USB充电"; break; } sb.append("\n充电方式:").append(BatteryStatus2); StringBatteryTemp=null; switch(intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)){ caseBatteryManager.BATTERY_HEALTH_UNKNOWN: BatteryTemp="未知错误"; break; caseBatteryManager.BATTERY_HEALTH_GOOD: BatteryTemp="状态良好"; break; caseBatteryManager.BATTERY_HEALTH_DEAD: BatteryTemp="电池没有电"; break; caseBatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: BatteryTemp="电池电压过高"; break; caseBatteryManager.BATTERY_HEALTH_OVERHEAT: BatteryTemp="电池过热"; break; } sb.append("\n电池状态:").append(BatteryTemp); log(sb.toString()); } } }; classSpinThreadextendsThread{ privatebooleanmStop; publicvoidquit(){ synchronized(this){ mStop=true; } } publicvoidrun(){ while(true){ synchronized(this){ if(mStop){ return; } } } } } }
2.main.xml:
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <CheckBoxandroid: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="电源测试" /> <ScrollViewandroid:id="@+id/scroll" android:layout_width="fill_parent" android:layout_height="0px" android:layout_weight="1" > <TextViewandroid: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:
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.lenovo.android" android:versionCode="1" android:versionName="1.0"> <uses-sdkandroid:minSdkVersion="8"/> <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"> <activityandroid:name=".PowerTestActivity" android:label="@string/app_name"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> <uses-permissionandroid:name="android.permission.WAKE_LOCK"/> <uses-permissionandroid:name="android.permission.DEVICE_POWER"/> </manifest>
希望本文所述对大家的Android程序设计有所帮助。