Windows Phone 8 allows any app to update the lock screen images in the background. Lets see how you can do this in your app.

First you need to declare that your app can act as a lock screen image provider. This is done by updating the Extensions section in the WMAppManifest.xml

Make sure you have the following xml in the Extensions element:

<Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" />

Ensure that you do this correctly. If you forget to add this element you will get an exception when you try to ask for the users permission to access this capability.

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ImageOnLockScreen.LockScreenSettings.<TrySetImageUri>d__0.MoveNext()

Once you have declared this capability, you can then use the following piece of code to set the lock screen image:

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    var result = await  LockScreenSettings.TrySetImageUri("Assets/Image1.jpg");

if (!result.IsSuccess) {/* There might be an exception or the user may have denied permission to your app. */ }

}

I have encapsulated the setting of the image and asking the permission in a simple static method. Here is the helper method:

public class LockScreenSettings
    {

public static async Task TrySetImageUri(string filePathOfTheImage, bool isAppResource=true)
        {
var result = new LockScreenImageResult();
            try
            {
var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication;
if (!isProvider)
                {
                    // If you're not the provider, this call will prompt the user for permission.
// Calling RequestAccessAsync from a background agent is not allowed.
                    var op = await Windows.Phone.System.UserProfile.LockScreenManager.RequestAccessAsync();

                    // Only do further work if the access was granted.
isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted;
                }

                //User denied permission to update lock screen
if (!isProvider) result.IsSuccess = false;

                // At this stage, the app is the active lock screen background provider.

                // The following code example shows the new URI schema.
// ms-appdata points to the root of the local app data folder.
// ms-appx points to the Local app install folder, to reference resources bundled in the XAP package.
var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute);

                // Set the lock screen background image.
Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);

                // Get the URI of the lock screen background image.
var currentImage = Windows.Phone.System.UserProfile.LockScreen.GetImageUri();

            }
            catch (System.Exception ex)
            {
                result.IsSuccess = false;
                result.Exception = ex;
            }

            return result;
        }

    }
public class LockScreenImageResult
    {
public LockScreenImageResult()
        {
            IsSuccess = true;
        }
public bool IsSuccess { get; set; }
public Exception Exception { get; set; }
    }

I have created an application which I would be posting on CodePlex soon.
Here are some screenshots

You can find more details on MSDN. I am in the process of updating my “WordPress for Windows Phone” app to include this capability as well, stay tuned…

Related Links

 

Advertisements

Share your thoughts

Please log in using one of these methods to post your comment:

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