WinRT - 如何判断设备是使用电池还是墙上供电?

时间:2013-10-06 23:36:53

标签: windows-runtime microsoft-metro windows-applications

假设我想定制我的应用程序以在使用电池供电时节省吞吐量,但是当它插入墙上插座时会完全钻孔。

是否有插入事件以检测何时发生这些事情,如检测到互联网连接?

2 个答案:

答案 0 :(得分:0)

显然你可以插入这个trigger。它仅在设备插入时才会启动。

答案 1 :(得分:0)

IE和WebView http://ie.microsoft.com/testdrive/HTML5/PowerMeter/Default.html可以轮询cpu电源状态 例如:http://apps.microsoft.com/windows/app/plugged-in/2b91b1e1-9e30-4a96-bc95-b196e46bef9d 在c#/ xaml中尝试:

XAML

<WebView x:Name="BatteryCheck_WV" Source="ms-appx-web:///Assets/JSBatteryCheck.html" Height="50" Width="50" Visibility="Collapsed"    />

C#

var PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer(async (timer) =>
            {
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low,
                async () =>
                {
                    if (Window.Current.Visible) BatteryStatus.IsOn= Convert.ToBoolean(await BatteryCheck_WV.InvokeScriptAsync("IsPluggedIn", null));

                });
            }, new TimeSpan(0, 0, 0, 1, 0));

HTML / JS

<!DOCTYPE html>
<html>
<head>
    <title>JSBatteryCheck</title>
    <script type="text/javascript">

        var batterytimeOut;
        var timeOutGo = true;
        var lastDisplayUpdateTime = new Date();
        var MilliSeconds = 1000;
        var displayUpdateInterval = 200;
        var values = [];
        var maxValues = displayUpdateInterval;

        var PluggedIn = true;
        var currentTicksPerMilliSecond = 0;

        function GetTicksPerSecond() {
            if ((new Date() - lastDisplayUpdateTime) >= displayUpdateInterval) {
                lastDisplayUpdateTime = new Date;
                currentTicksPerMilliSecond = Avg();
                values = [];
                if (!isNaN(currentTicksPerMilliSecond)) {

                    PluggedIn = (currentTicksPerMilliSecond > 9) ? false : true;
                }
            }
            else Tick();

            if (timeOutGo) batterytimeOut = setTimeout(function () { GetTicksPerSecond() }, 1);
        }

        function StartTimer() {
            timeOutGo = true;
            GetTicksPerSecond();
            return "";
        }

        function Tick() {
            if (values.length > maxValues) values.shift();
            else values.push(new Date());
        }

        function Avg() {
            if (values.length > 1) {
                var earliest = values[0];
                var latest = values[values.length - 1];
                var elapsed = latest - earliest;
                var elapsedSeconds = elapsed / MilliSeconds;
                var avg = MilliSeconds / ((values.length - 1) / elapsedSeconds);
                return parseInt(avg);
            }
            else return NaN;
        }

        function StopTimeOut() {
            clearTimeout(batterytimeOut);
            batterytimeOut = null;
            timeOutGo = false;
        }


        function IsPluggedIn() {
            return String(PluggedIn);
        }

    </script>
</head>
<body onload="GetTicksPerSecond()">
    <div id="PowerStatus"></div>
    <button onclick="StopTimeOut()">Stop timeout</button>
</body>
</html>

对于后台任务,SystemTriggerType.BackgroundWorkCostChange事件最好,但需要锁定屏幕访问