1

假设我想定制我的应用程序以在使用电池电源运行时限制吞吐量,但当它插入墙上插座时会全力以赴。

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

4

2 回答 2

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 事件是最好的,但需要锁屏访问

于 2014-02-13T14:53:46.800 回答
0

显然你可以插入这个触发器。它仅在设备插入时才会触发。

于 2013-10-08T14:45:48.663 回答