How to get timer running even when application is running in background or phone is locked in Windows Phone

ⅰ亾dé卋堺 提交于 2019-12-24 13:47:32

问题


I have a timer in my application in Windows Phone 7.1 implemented using

DispatcherTimer _timer;

Initialized as

Sample._timer = new DispatcherTimer();
Sample._timer.Interval = new TimeSpan(0, 0, 1);
Sample._timer.Tick += new EventHandler(Timer_Tick);
Sample._timer.Start();

    private void Timer_Tick(object sender, EventArgs e)
    {

        double newValue = Sample.Value + 1.686;
        if (newValue >= 100)
            newValue = 0;
        Sample.Value = newValue;
        txtDigitalClock.Text = GetTime();
    }
    public string GetTime()
    {
        time += TimeSpan.FromSeconds(1);
        return string.Format("{0:D2}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
    }

This is working fine in normal condition

Here is my problem

1) Timer is not running when phone is in locked state(screen is loced)

2) Timer is not running when application is running in background (When you press start button in windows phone the app goes to background).

any help would be greatly appreciated..


回答1:


To run your App (and Timer) under lock screen, you have to disable ApplicationIdleDetectionMode.
If you don't disable idle your App will stop as it is said at MSDN:

This event (Deactivation) is also raised if the device’s lock screen is engaged, unless application idle detection is disabled.

If you want to run Timer in the background (eg. after pressing Start buton), you won't be able to do this as MSDN says:

When the user navigates forward, away from an app, after the Deactivated event is raised, the operating system will attempt to put the app into a dormant state. In this state, all of the application’s threads are stopped and no processing takes place, but the application remains intact in memory.

The bigger problem is when your App is tombstoned - the app doesn't ramain (all) in memory.

You can try to do your job with Background Agents, but that is other story.

Also remember about Certification requirements when your App disables Idle or uses Background Agent.

Similar problem was here.




回答2:


I searched for your question on google (cuz I'm not into Winphone) and found

http://stackoverflow.com/questions/8352515/how-can-i-run-my-windows-phone-application-in-background

apparently it simply is not possible. I hope this answers your question




回答3:


Please write below line timer Initialization

ApplicationIdleModeHelper.Current.HasUserAgreedToRunUnderLock = true;



回答4:


I resolved this issue by saving the starting timer of the value on isolated storage

 IsolatedStorageSettings.ApplicationSettings.Add("TimerStarted",DateTime.UtcNow);

And when the app is reactivated after going to background, i will look for this value in isolated storage and use that to show the timer



来源:https://stackoverflow.com/questions/21072975/how-to-get-timer-running-even-when-application-is-running-in-background-or-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!