Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP

Reading this article, we can learn how to How to Keep your screen asleep while your application running in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in cross-platform application development.

The ScreenLock class is available at Xamarin.Essentials API. It can request to keep the screen from falling asleep when the application is running. Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.

Reading this article, you can learn how to how to keep your screen asleep while your application is running in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in cross-platform application development.

The following important tools are required for developing Xamarin Forms App,

  1. Windows 10 (Recommended)
  2. Visual Studio 2017
  3. Android API 19 or higher and UWP 10.0.16299.0 or higher.

 

Now, we can discuss step-by-step App development.

Step 1

Open Visual Studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give the Suitable Name for your App (XamFormScreenLock) ->OK

Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 2

Select the Cross-Platform template as a Blank APP ->Set Platform as Android and UWP and code sharing strategy as .NET standard, Afterwards, Visual Studio creates 3 projects (Portable, Android, UWP)

Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 3

For adding a reference,

RightClick Your solution (XamFormDialer) and Select Manage NuGet Packages.

For adding Xamarin.Essentials Reference, Choose Browse and Search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.

Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 4

Add the Label and Button controls in Mainpage.Xaml for displaying Title, lock status and change status button.

  1. <Label FontAttributes="Bold" Text="Screen Lock Active or Release in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>  
  2.    <Label FontAttributes="Bold" x:Name="lblScrStat" Text="" />  
  3. <Button x:Name="btnChangeLock" Text="Change Lock status" Clicked="btnChangeLock_Click"/>  

 

Step 5

Add the following code in the MainActivity.cs in XamFormScreenLock.Android project,

In onCreate() method,

  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState);  

Also add the below method,

  1. publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {  
  2.     Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  3.     base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  4. }  

Step 6

Add the following namespace and code in Mainpage.Xaml.cs

  1. using Xamarin.Essentials;  
  2. protectedoverridevoid OnAppearing() {  
  3.     base.OnAppearing();  
  4. }  
  5. void btnChangeLock_Click(object sender, System.EventArgs e) {  
  6.     ToggleScreenLock();  
  7. }  
  8. publicvoid ToggleScreenLock() {  
  9.     try {  
  10.         if (!ScreenLock.IsActive) {  
  11.             ScreenLock.RequestActive();  
  12.             lblScrStat.Text = "Screen Lock Status is " + (ScreenLock.IsActive).ToString() + " and Request active is working ";  
  13.         }  
  14.         elseif(ScreenLock.IsActive) {  
  15.             ScreenLock.RequestRelease();  
  16.             lblScrStat.Text = "Screen Lock Status is " + (ScreenLock.IsActive).ToString() + " and Request Release is working";  
  17.         }  
  18.     } catch (Exception ex) {  
  19.         lblScrStat.Text = "NOt working";  
  20.         // Other error has occurred.  
  21.     }  
  22. }  

Note

Automatically the following code will be generated in XAML code view, while we are done in the design view.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormScreenlock" x:Class="XamFormScreenlock.MainPage">  
  3.     <StackLayout>  
  4.         <Label FontAttributes="Bold" Text="Screen Lock Active or Release in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Label FontAttributes="Bold" x:Name="lblScrStat" Text="" />  
  6.         <Button x:Name="btnChangeLock" Text="Change Lock status" Clicked="btnChangeLock_Click" />  
  7.     </StackLayout>  
  8. </ContentPage>  

Step 7

Deploy of your App in UWP and Android. The output of the XamFormScreenLock App is:

With Screen Lock Request Release in Android,

Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP
 
When the Screen Lock Time is after 1 Minute,
 
Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
With Screen Lock Request Active, after clicking the CHANGE LOCK STATUS button in Android,
 
Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
When the Screen Lock Time is after 1 Minute, 
 
Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

With Screen Lock Request Active, after clicking the CHANGE LOCK STATUS button in Android,

Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP

With Screen Lock Request Release in Android,

Screen Lock Active Or Release In Xamarin Forms Application Using Xamarin Essentials For Android And UWP
 
Summary

Now, you have successfully tested screen lock status changes in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.