扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As 电池状态信息) As Long
创新互联专注于咸丰企业网站建设,成都响应式网站建设公司,商城网站建设。咸丰网站建设公司,为咸丰等地区提供建站服务。全流程按需网站制作,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
Private Type 电池状态信息
外接电源状态 As Byte '没插=0 插了=1 无法取得信息=255
电池状态 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 状态更新_Timer
Call 窗体透明((Me.hwnd), (170))
End Sub
Private Sub 状态更新_Timer()
Dim 信息 As 电池状态信息
GetSystemPowerStatus 信息
'[头]外接电源状态
If (信息.外接电源状态) = (1) Then
Label1.Caption = ("有")
ElseIf (信息.外接电源状态) = (0) Then
Label1.Caption = ("无")
ElseIf (信息.外接电源状态) = (255) Then
Label1.Caption = ("状态读取错误")
End If
'[尾]外接电源状态
'[头]电池状态
If (信息.电池状态) = (8) Or (信息.电池状态) = (9) Then
Label3.Caption = ("充电中")
ElseIf (信息.电池状态) = (128) Then
Label3.Caption = ("没有电池")
ElseIf (信息.电池状态) = (255) Then
Label3.Caption = ("状态读取错误")
Else
Label3.Caption = ("使用中")
End If
'[尾]电池状态
'[头]剩余电量
If (信息.电池剩余电量) = (100) Then
Me.Caption = ("剩余电量:") (信息.电池剩余电量) ("%")
Label5.Caption = (信息.电池剩余电量) ("%")
电池电量进度条.Value = (信息.电池剩余电量)
电池电量进度条.Visible = (True)
ElseIf (信息.电池剩余电量) = (255) Then
Label5.Caption = ("状态读取错误")
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; //电池状态
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 = "充电状态";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
BatteryStatus = "放电状态";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
BatteryStatus = "未充电";
break;
case BatteryManager.BATTERY_STATUS_FULL:
BatteryStatus = "充满电";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
BatteryStatus = "未知道状态";
break;
}
switch (intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)) {
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
BatteryTemp = "未知错误";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
BatteryTemp = "状态良好";
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 +
"℃"
);
}
},
});
// 注册一个系统 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("关闭广播监听");
}
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(); }}
比较简单的实现方式
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流