我正在用 C# 编写一个语音识别程序,我已经编译了几行代码,当我说“电池电量”时,它们会说出当前的电池电量。
if (e.Result.Text.ToLower() == "battery level")
{
System.Management.ManagementClass wmi = new System.Management.ManagementClass("Win32_Battery");
var allBatteries = wmi.GetInstances();
//String estimatedChargeRemaining = String.Empty;
int batteryLevel = 0;
foreach (var battery in allBatteries)
{
batteryLevel = Convert.ToInt32(battery["EstimatedChargeRemaining"]);
}
if(batteryLevel < 25)
JARVIS.Speak("Warning, Battery level has dropped below 25%");
else //Guessing you want else
JARVIS.Speak("The battery level is at: " + batteryLevel.ToString() + "%");
return;
}
而不是这条线只在我说“电池电量”时发生,我希望它每 15 分钟自动检查一次电池电量,并在电池电量低于 25% 时通过语音自动报告给我:
if(batteryLevel < 25)
JARVIS.Speak("Warning, Battery level has dropped below 25%");
我猜我需要一个计时器,但除此之外我不知道。
谢谢。