×
Namespaces

Variants
Actions
(Difference between revisions)

Dynamic Lock Screen for Windows Phone 8

From Nokia Developer Wiki
Jump to: navigation, search
chintandave_er (Talk | contribs)
(Chintandave er -)
chintandave_er (Talk | contribs)
(Chintandave er -)
Line 123: Line 123:
 
Once you say 'yes', This app becomes current background image provider of Lock screen and we can see the image set by above method in lock screen by pressing F12 during using in WP8 Simulator.  
 
Once you say 'yes', This app becomes current background image provider of Lock screen and we can see the image set by above method in lock screen by pressing F12 during using in WP8 Simulator.  
  
[[File:LockscreenSet_Dl.png|320x540px]]
+
<gallery widths="540px" heights="320px">
 +
File::LockscreenSet_Dl.png|Image set on Lock screen
 +
File:Home_DL.png|Home page
 +
</gallery>
  
 
To call set lock screen image, call above method as below. Here we need to pass Image file path that we want to set as Lock screen and whether it is in app resources folder or in root.
 
To call set lock screen image, call above method as below. Here we need to pass Image file path that we want to set as Lock screen and whether it is in app resources folder or in root.

Revision as of 11:53, 11 November 2012

This article explains how to change the Lock screen image of Windows phone 8 using application that can be set in Lock screen setting. Going further, this article also explain how we can change the lock screen wallpaper periodically with new image. This article also explains how to add text or image at runtime over Jpeg image to add more information on Lock screen wallpaper and compose image at runtime.

Note.pngNote: This is a community entry in the Windows Phone 8 Wiki Competition 2012Q4.

WP Metro Icon File.png
WP Metro Icon UI.png
WP Metro Icon Web.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata
Tested with
Devices(s): Windows Phone 8 device, Nokia Lumia 920, 820
Compatibility
Platform(s): Windows Phone 8
Windows Phone 8
Device(s): Windows Phone 8 Only
Article
Keywords: LockScreenManager, Background Agent
Created: chintandave_er (06 Nov 2012)
Last edited: chintandave_er (11 Nov 2012)

Contents

Introduction

Windows phone 8 has awesome features like new personalised home screen, Dynamic Lock screen, Integrated VOIP Skype etc. In Windows Phone 8, a user can choose to use an app as its lock screen background image provider. I think best feature is Dynamic Lock Screen as user first see phone is Lock screen image and which can have lots of information.

This article explains how to change the Lock screen image of Windows phone 8 using LockScreenManager. We will also show you how to change the Lockscreen wallpaper in background periodically using Background Agent Scheduler. This article also explains how to compose image dynamically by adding text or image at runtime over Jpeg image to add more information on Lock screen wallpaper.

To provide the lock screen background image in the phone, we need to declare the app's intent in the app manifest file, and add code to handle changing the background image and then to change image periodically need to use Windows phone Scheduler as Background Agent.

First we will show you how to set a default image to lock screen by app acting as Background image provide.

Setting up a default lock screen background image

To provide the lock screen background image for the phone, first we need to update the app manifest file to declare your app as a lock screen background provider.

Updating the app manifest file

To Update the app manifest file so that app can access the lock screen of the phone, we need to update WMAppManifest.xml file as below.

  1. In Solution Explorer, expand Properties, right-click WMAppManifest.xml, click Open With, and then click Source Code (Text Editor) With Encoding.
  2. Add the lock screen background <Extension> element in the <Extensions> element. If the <Extensions> element doesn’t appear in the file, place the entire code example below into the file. The <Extensions> element must be placed below the <Tokens> element.
<Extensions>
<Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" />
</Extensions>

Now users can see the app in the Lock Screen Settings and choose to let the app change the lock screen.

Adding code to change the Lock screen background

We can change background of Lock screen using Windows.Phone.System.UserProfile.LockScreenManager namespace. To change the background, our app must have selected as Lock screen background provide, to check that Go to Setting -> Lock Screen.

Setting DL.png

To check that whether our app is selected as Lock screen image provider or not through code, we can do that using below code.

if (!LockScreenManager.IsProvidedByCurrentApplication)
{
// If you're not the provider, this call will prompt the user for permission.
// Calling RequestAccessAsync from a background agent is not allowed.
await LockScreenManager.RequestAccessAsync();
}
 
// Only do further work if the access is granted.
if (LockScreenManager.IsProvidedByCurrentApplication)
{
// change the background here.
}

This code ask user to give permission for the app to provide wallpaper images to the lock screen. Once user click on OK, our app is granted to change the lock screen and become current Lock screen image provider.

AppAsk DL.png

Then, we need to call the LockScreen.SetImageUri method with the URI of the picture. The URI must be prefixed by “ms-appx:///” if the picture is stored in the resources, or “ms-appdata:///Local/” if the picture is stored in root folder.

  • "ms-appx:///" for resources bundled within the xap
  • "ms-appdata:///local/" for local images within the root folder.

The following final code example shows how we can change the lock screen background image. We need to call below function. Here method uses async as 'the user for permission can only be done by using an async call.

private async void LockScreenChange(string filePathOfTheImage, bool isAppResource)
{
if (!LockScreenManager.IsProvidedByCurrentApplication)
{
// If you're not the provider, this call will prompt the user for permission.
// Calling RequestAccessAsync from a background agent is not allowed.
await LockScreenManager.RequestAccessAsync();
}
 
// Only do further work if the access is granted.
if (LockScreenManager.IsProvidedByCurrentApplication)
{
// 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.
LockScreen.SetImageUri(uri);
 
// Get the URI of the lock screen background image.
var currentImage = LockScreen.GetImageUri();
System.Diagnostics.Debug.WriteLine("The new lock screen background image is set to {0}", currentImage.ToString());
MessageBox.Show("Lock screen changed. Click F12 or go to lock screen.");
}
else
{
MessageBox.Show("Background cant be updated as you clicked no!!");
}
}

Once you say 'yes', This app becomes current background image provider of Lock screen and we can see the image set by above method in lock screen by pressing F12 during using in WP8 Simulator.

To call set lock screen image, call above method as below. Here we need to pass Image file path that we want to set as Lock screen and whether it is in app resources folder or in root.

 LockScreenChange("wallpaper/CustomizedPersonalWalleper_0.jpg", true);
2775 page views in the last 30 days.
×