Requesting Lock Screen Access In Windows 8


In Windows 8,Lock Screen has been made an elite area of the apps.If your app is on the users lock screen you have the elite privilege of time triggering your background task and push notification trigger.

But even if you don’t have the lock screen access,you can still use the system trigger and maintenance trigger for triggering your tasks.

So how do you ask for lock screen access?

var status = BackgroundExecutionManager.GetAccessStatus();
 if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.Denied)
                status = await BackgroundExecutionManager.RequestAccessAsync();
            string result;
            switch (status)
            {
                case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
                    result = "This app is on the lock screen and 
has access to Always-On Real Time Connectivity.";
                    break;
                case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
                    result = "This app is on the lock screen and 
has access to Active Real Time Connectivity.";
                    break;
                case BackgroundAccessStatus.Denied:
                    result = "This app is not on the lock screen.";
                    break;
                case BackgroundAccessStatus.Unspecified:
                    result = "The user has not yet taken any action. 
This is the default setting and the app is not on the lock screen.";
                    break;
            }

Now if you try and debug this with a breakpoint .You might end up with an error that looks like

An unhandled exception of type ‘System.Exception’ occurred in mscorlib.dll Additional information: Element not found. (Exception from HRESULT: 0x8002802B (TYPE_E_ELEMENTNOTFOUND))

.

There is nothing to worry about though.

There are two bugs that can be  when accessing for a lockscreen access. First one, if you have breakpoint on RequestAccessAsync line, then the execution will fail because your application is not running in foreground (you are in Visual Studio, not in your app) and the lockscreen dialog cannot find main windows of your app.

Another problem occurs when running in Simulator – every call on GetAccessStatus throws an exception, because this call is basically not allowed in Simulator.

If you want to debug this, then place your breakpoint after the GetAccessStatus call and test it on Local machine and it should just work.

A caveat while using this make sure that you call this from the UI thread .Type library errors can be induced when a call needs to marshaled to another thread. Be very sure that you only run this code on the program’s main UI thread.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s