Raj
Raj

Reputation: 11

how to lock the screen programmatically in windows phone 8?

We can prevent the screen to lock using the below code

PhoneApplicationService.Current.ApplicationIdleMode = IdleDetectionMode.Disabled 

and

PhoneApplicationService.Current.UserIdleDetectionMode= IdleDetectionMode.Disabled 

but how to lock the screen from my app. Like the below app

http://www.windowsphone.com/en-us/store/app/one-touch-lockscreen/a3b1220b-1f9a-4bf0-93bc-21ed02792279

Thanks in advance

Upvotes: 1

Views: 1694

Answers (3)

vilicvane
vilicvane

Reputation: 12413

What @yasen wrote is correct.

[DllImport("ShellChromeAPI.dll")]
private extern static void Shell_TurnScreenOn(bool value);

I've tried the following cases:
Runtime 8.1 C# (Passed store certification)
Runtime 8.1 C++ with Runtime Component 8.1 C# (Haven't tried to publish this in store)
Silverlight/DirectX 8.0 C++ (Passed store certification)

Here's the link to my app that's using the last solution mentioned above.
http://www.windowsphone.com/s?appid=38bf5918-025e-4f23-b515-2cac451a84ab

And I've heard about cases in store using Silverlight that supports 8.0 and 8.1.

Upvotes: 2

Amit Bhatiya
Amit Bhatiya

Reputation: 2621

You can get Screen is locked or not by Windows.Phone.System.SystemProtection.ScreenLocked

but Unfortunately There is no way to lock the screen via code in Windows Phone 7.x or 8.

Upvotes: 1

yasen
yasen

Reputation: 3580

It's pretty hacky. It's not in the official API, so it could stop working at any time, just like the volume control API. Anyway, it you want to do it, you need to use this external method:

[System.Runtime.InteropServices.DllImport("ShellChromeAPI.dll")]
private extern static void Shell_TurnScreenOn(bool value);

For WP8.0 app this needs to be in a Windows Runtime Component (you should reference its output, as the project cannot be referenced).

From what I understand, though, this won't work on WP8.1 devices, so you'll need a separate WP8.1 app and I think it needs to be a XAML (Windows Store) app.

Upvotes: 2

Related Questions