Private?Declare?Function?GetSystemPowerStatus?Lib?"kernel32"?(lpSystemPowerStatus?As?電池狀態(tài)信息)?As?Long
創(chuàng)新互聯(lián)專注于咸豐企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設公司,商城網(wǎng)站建設。咸豐網(wǎng)站建設公司,為咸豐等地區(qū)提供建站服務。全流程按需網(wǎng)站制作,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
Private?Type?電池狀態(tài)信息
外接電源狀態(tài)?As?Byte?'沒插=0??插了=1??無法取得信息=255
電池狀態(tài)?As?Byte?'電量大于66%=1?電小于33%=2?小于5%=4?充電中=8?沒有電池=128??無法取得信息=255
'以上說明似乎不對,以下是自己測試的:
'電量大于66%=1
'電量小于33%=???
'電量小于5%=???
'充電中=9
'沒有電池=128
電池剩余電量?As?Byte?'0~100,若未知則為255
保留?As?Byte?'必須為0
秒為單位的電池剩余電量?As?Integer?'若未知則為-1
秒為單位的電池充滿電的電量?As?Integer?'若未知則為-1
End?Type
Private?Sub?Form_Load()
Call?狀態(tài)更新_Timer
Call?窗體透明((Me.hwnd),?(170))
End?Sub
Private?Sub?狀態(tài)更新_Timer()
Dim?信息?As?電池狀態(tài)信息
GetSystemPowerStatus?信息
'[頭]外接電源狀態(tài)
If?(信息.外接電源狀態(tài))?=?(1)?Then
Label1.Caption?=?("有")
ElseIf?(信息.外接電源狀態(tài))?=?(0)?Then
Label1.Caption?=?("無")
ElseIf?(信息.外接電源狀態(tài))?=?(255)?Then
Label1.Caption?=?("狀態(tài)讀取錯誤")
End?If
'[尾]外接電源狀態(tài)
'[頭]電池狀態(tài)
If?(信息.電池狀態(tài))?=?(8)?Or?(信息.電池狀態(tài))?=?(9)?Then
Label3.Caption?=?("充電中")
ElseIf?(信息.電池狀態(tài))?=?(128)?Then
Label3.Caption?=?("沒有電池")
ElseIf?(信息.電池狀態(tài))?=?(255)?Then
Label3.Caption?=?("狀態(tài)讀取錯誤")
Else
Label3.Caption?=?("使用中")
End?If
'[尾]電池狀態(tài)
'[頭]剩余電量
If?(信息.電池剩余電量)?=?(100)?Then
Me.Caption?=?("剩余電量:")??(信息.電池剩余電量)??("%")
Label5.Caption?=?(信息.電池剩余電量)??("%")
電池電量進度條.Value?=?(信息.電池剩余電量)
電池電量進度條.Visible?=?(True)
ElseIf?(信息.電池剩余電量)?=?(255)?Then
Label5.Caption?=?("狀態(tài)讀取錯誤")
163????????Me.Caption?=?("電池剩余電量讀取錯誤")
164????????電池電量進度條.Visible?=?(False)
165????End?If
166????'[尾]剩余電量
167????End?Sub
importClass(android.content.Intent);
importClass(android.content.IntentFilter);
importClass(android.os.BatteryManager);
let?BatteryN;?//目前電量
let?BatteryV;?//電池電壓
let?BatteryT;?//電池溫度
let?BatteryStatus;?//電池狀態(tài)
let?BatteryTemp;?//電池使用情況
let?mBatInfoReceiver?=?new?JavaAdapter(android.content.BroadcastReceiver,?{
onReceive:?function?(context,?intent)?{
log(intent);
let?action?=?intent.getAction();
/*
*?如果捕捉到的action是ACTION_BATTERY_CHANGED,?就運行onBatteryInfoReceiver()
*/
if?(Intent.ACTION_BATTERY_CHANGED.equals(action))?{
BatteryN?=?intent.getIntExtra("level",?0);?//目前電量
BatteryV?=?intent.getIntExtra("voltage",?0);?//電池電壓
BatteryT?=?intent.getIntExtra("temperature",?0);?//電池溫度
switch?(intent.getIntExtra("status",?BatteryManager.BATTERY_STATUS_UNKNOWN))?{
case?BatteryManager.BATTERY_STATUS_CHARGING:
BatteryStatus?=?"充電狀態(tài)";
break;
case?BatteryManager.BATTERY_STATUS_DISCHARGING:
BatteryStatus?=?"放電狀態(tài)";
break;
case?BatteryManager.BATTERY_STATUS_NOT_CHARGING:
BatteryStatus?=?"未充電";
break;
case?BatteryManager.BATTERY_STATUS_FULL:
BatteryStatus?=?"充滿電";
break;
case?BatteryManager.BATTERY_STATUS_UNKNOWN:
BatteryStatus?=?"未知道狀態(tài)";
break;
}
switch?(intent.getIntExtra("health",?BatteryManager.BATTERY_HEALTH_UNKNOWN))?{
case?BatteryManager.BATTERY_HEALTH_UNKNOWN:
BatteryTemp?=?"未知錯誤";
break;
case?BatteryManager.BATTERY_HEALTH_GOOD:
BatteryTemp?=?"狀態(tài)良好";
break;
case?BatteryManager.BATTERY_HEALTH_DEAD:
BatteryTemp?=?"電池沒有電";
break;
case?BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
BatteryTemp?=?"電池電壓過高";
break;
case?BatteryManager.BATTERY_HEALTH_OVERHEAT:
BatteryTemp?=?"電池過熱";
break;
}
log(
"目前電量為"?+
BatteryN?+
"%?---?"?+
BatteryStatus?+
"\n"?+
"電壓為"?+
BatteryV?+
"mV?---?"?+
BatteryTemp?+
"\n"?+
"溫度為"?+
BatteryT?*?0.1?+
"℃"
);
}
},
});
//?注冊一個系統(tǒng)?BroadcastReceiver,作為訪問電池計量之用這個不能直接在AndroidManifest.xml中注冊
let?filter?=?new?IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
context.registerReceiver(mBatInfoReceiver,?filter);
events.on("exit",?function?()?{
unregisterReceiver();
});
function?unregisterReceiver()?{
context.unregisterReceiver(mBatInfoReceiver);
toastLog("關閉廣播監(jiān)聽");
}
setInterval(function?()?{},?1000);
/**手機類*/public class Phone{ private double price; private String brand; private Battery battery; private double weight; public Phone(Battery battery){ this.battery = battery; } public void play(){ System.out.println("播放"); } public void charge(){ battery.charge(); }}/**電池類*/public class Battery{ private String brand; private double price; private int year; public void charge(){ System.out.println("充電"); }} /**測試類*/public class Test{ public static void main(String[] args){ Battery battery = new Battery(); Phone phone = new Phone(battery); phone.charge(); }}
比較簡單的實現(xiàn)方式