Saturday, November 16, 2013

Working with windows phone 8 Lock Screen

Just turned off the TV, was watching team India celebrating their series win over West Indies. The entire series was remarkable and overwhelming, not only because India won, but also because it was the last series of Sachin’s career. Yes, you heard it right, Sachin Ramesh Tendulkar has retired.
I have therefore decided to give a tribute to him, by writing a post describing how to manipulate phone’s lock screen by setting Sachin photos on it through code.

Lock Screen

Windows phone don’t have a desktop background. It only have a lock screen where you can set your wallpaper or whatsoever you like.  What you can do is go to any one of your album, choose a photo and from settings, set it as your lock screen. But what if you need to create an application, and want it to do the same for you? Interesting right? So let’s see how we can achieve this.

The Image Source

There is a catch though. You cannot address the image directly from phone, you need to first copy the image to Isolated storage instead and address it from there. You can have a look to my previous post on Isolated Storage to get an idea on how to do that. Here is the simple method which set’s an image as lock screen.

   1: /// <summary>
   2: /// Set the Specified image path as the Phone's lock screen.
   3: /// </summary>
   4: /// <param name="filePathOfTheImageFromIsolatedStorage"></param>
   5: /// <param name="isAppResource">If the image is embeded inside the
   6: /// project in XAP itself, set it to yes</param>
   7: private async void LockHelper(string filePathOfTheImageFromIsolatedStorage, 
   8:                 bool isAppResource)
   9: {
  10: try
  11: {
  12:     var isProvider = LockScreenManager.IsProvidedByCurrentApplication;
  13:     if (!isProvider)
  14:     {                    
  15:         var operationRequest = await LockScreenManager.RequestAccessAsync();
  16:         isProvider = (operationRequest == LockScreenRequestResult.Granted);
  17:     }
  18:  
  19:     if (isProvider)
  20:     {                    
  21:         var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/";
  22:         var uri = new Uri(schema + 
  23:                     filePathOfTheImageFromIsolatedStorage, UriKind.Absolute);
  24:  
  25:         // Set the lock screen background image.
  26:         LockScreen.SetImageUri(uri);
  27:  
  28:         // Get the URI of the lock screen background image.
  29:         var currentImage = LockScreen.GetImageUri();                    
  30:     }              
  31: }
  32: catch (Exception ex)
  33: {
  34:     MessageBox.Show("Unable to update image." + ex.Message);
  35: }
  36: }


Here is the jest of what this method actually do. It first checks whether the application has the permission of accessing and modifying the lock screen on not. You need to provide the permission to the application to access the lock screen. We will shortly see how to do that.

Next if the permit is there, the method than seek for the permission of the user to allow the app to manipulate the lock screen. It will popup a dialog box for the user to allow the permission.

If the permission is granted, the method then will create a Uri based on the image path provided. It will prepend the path with “ms-appdata:///Local/” in case the image is in isolated storage, or with “ms-appx:///” if the image is embedded in the solution itself. Then the image is set as the lock screen wallpaper via SetImageUri() method using the Uri thus created.

Application Manifestation

To allow the application to manipulate lock screen, we need to modify the “WMAppManifest.xml” file. Right click the file, choose open with and select the “Source Code (text) editor with encoding” option. Once the file is open, search for the Tokens tag and place the following lines after the end of the tag:

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

That’s it, you are done! Just pass the values in the function and you will be greeted with a new custom lock screen, every time. Just like mine:

wp_ss_20131116_0001

Happy Coding!

Thank you Sachin!

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top