Xamarin Forms - Disable auto-lock when the app is open





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to disable auto-lock when my app is open. How can I do that?










share|improve this question























  • this will be platform specific: stackoverflow.com/questions/12661004/…

    – Jason
    Nov 29 '18 at 6:22











  • developer.android.com/training/scheduling/wakelock

    – Jason
    Nov 29 '18 at 6:23


















0















I want to disable auto-lock when my app is open. How can I do that?










share|improve this question























  • this will be platform specific: stackoverflow.com/questions/12661004/…

    – Jason
    Nov 29 '18 at 6:22











  • developer.android.com/training/scheduling/wakelock

    – Jason
    Nov 29 '18 at 6:23














0












0








0








I want to disable auto-lock when my app is open. How can I do that?










share|improve this question














I want to disable auto-lock when my app is open. How can I do that?







xamarin xamarin.forms xamarin.ios xamarin.android






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 29 '18 at 6:09









Lawrence AgultoLawrence Agulto

1239




1239













  • this will be platform specific: stackoverflow.com/questions/12661004/…

    – Jason
    Nov 29 '18 at 6:22











  • developer.android.com/training/scheduling/wakelock

    – Jason
    Nov 29 '18 at 6:23



















  • this will be platform specific: stackoverflow.com/questions/12661004/…

    – Jason
    Nov 29 '18 at 6:22











  • developer.android.com/training/scheduling/wakelock

    – Jason
    Nov 29 '18 at 6:23

















this will be platform specific: stackoverflow.com/questions/12661004/…

– Jason
Nov 29 '18 at 6:22





this will be platform specific: stackoverflow.com/questions/12661004/…

– Jason
Nov 29 '18 at 6:22













developer.android.com/training/scheduling/wakelock

– Jason
Nov 29 '18 at 6:23





developer.android.com/training/scheduling/wakelock

– Jason
Nov 29 '18 at 6:23












3 Answers
3






active

oldest

votes


















1














For iOS, you need to override the DidFinishLaunchingWithOptions method in your Appdelegate class:



 public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
UIApplication.SharedApplication.IdleTimerDisabled = true;
....
}


For Android, you need to do the following things in your MainActivity for it:



you have to declare this uses-permission on AndroidManifest:



<uses-permission android:name="android.permission.WAKE_LOCK" />


Create a global field for WakeLock using static Android.OS.PowerManager;;



private WakeLock wakeLock;


And in your OnResume:



PowerManager powerManager = (PowerManager)this.GetSystemService(Context.PowerService);
WakeLock wakeLock = powerManager.NewWakeLock(WakeLockFlags.Full, "My Lock");
wakeLock.Acquire();


Just remember to release this lock when your application is paused or destroyed by doing this:



wakeLock.Release();


Usually, it's suggested to call the acquire method inside the onResume() of your activity and the release method in onPause(). This way we guarantee that our application still performs well in the case of being paused or resumed.



Goodluck revert in case of queries






share|improve this answer
























  • I saw an article about using xamarin essentials i dont know if that solution is implemented in all my views or do I have to implement it one by one

    – Lawrence Agulto
    Nov 29 '18 at 6:37











  • the article is here : docs.microsoft.com/en-us/xamarin/essentials/…

    – Lawrence Agulto
    Nov 29 '18 at 6:38











  • You have to implement it in just one, MainActivity for Android and Appdelegate for iOS!! Are you sure you read the answer?

    – G.hakim
    Nov 29 '18 at 6:44













  • I mean the code in the article do I have to implement it once or one by one?

    – Lawrence Agulto
    Nov 29 '18 at 6:47











  • I am not sure i have not used this API of Xamarin Essentials but i have posted the answer of what i have used and what works well for me

    – G.hakim
    Nov 29 '18 at 6:48



















1














Xamarin.Forms:



//show something important, do not sleep
DependencyService.Get<INativeTasks>().ExecuteTask("cannotSleep");

//can put in OnDisappearing event
DependencyService.Get<INativeTasks>().ExecuteTask("canSleep");


Native tasks helper:



 public interface INativeTasks
{
...
void ExecuteTask(string task, object param=null);
...
}


Android:



Global variables and other..



public class DroidCore
{
private static DroidCore instance;
public static DroidCore Current
{
get { return instance ?? (instance = new DroidCore()); }
}

public static Window MainWindow { get; set; }
...
}


MainActivity.cs



protected override void OnCreate(Bundle bundle)
{
...
DroidCore.Current.MainView = this.Window.DecorView;
...
}


Native helpers:



public class NativeTasks : INativeTasks
{
public void ExecuteTask(string task, object param = null)
{
switch (task)
{

... //any native stuff you can imagine

case "cannotSleep":
DroidCore.MainWindow.AddFlags(WindowManagerFlags.KeepScreenOn);
break;

case "canSleep":
DroidCore.MainWindow.ClearFlags(WindowManagerFlags.KeepScreenOn);
break;
}
}
}


iOS:



Native helpers:



public class NativeTasks : INativeTasks
{
public void ExecuteTask(string task, object param = null)
{
switch (task)
{

... //any native stuff you can imagine

case "cannotSleep":
UIApplication.SharedApplication.IdleTimerDisabled = true;
break;

case "canSleep":
UIApplication.SharedApplication.IdleTimerDisabled = false;
break;
}
}
}





share|improve this answer
























  • I am curious about something here! Why write a renderer for something like this? if you want an application wide disabled auto-lock like the questioner has asked for just curious!

    – G.hakim
    Nov 29 '18 at 8:27













  • Native tasks are just for everything you can't do in forms. Have many other tasks implemented, posted the part you were asking for. Now when you hit the Forms wall with any native problem just put your native code inside INativeTasks and fly. :)

    – Nick Kovalsky
    Nov 29 '18 at 8:39



















0














We can achieve it by using Xamarin.Essentials plugin. Install it on solution level(While installing select include prerelease checkbox).



In your MainPage write this code



public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
ToggleScreenLock();
}
public void ToggleScreenLock()
{
if (!ScreenLock.IsActive)
ScreenLock.RequestActive();
else
ScreenLock.RequestRelease();
}
}


In Android project MainActivity add this line



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


before calling LoadApplication(new App());. For more information visit Microsoft docs.



This is working throughout the app. To install plugin refer below screenshot -



enter image description here






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53532836%2fxamarin-forms-disable-auto-lock-when-the-app-is-open%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    For iOS, you need to override the DidFinishLaunchingWithOptions method in your Appdelegate class:



     public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
    {
    UIApplication.SharedApplication.IdleTimerDisabled = true;
    ....
    }


    For Android, you need to do the following things in your MainActivity for it:



    you have to declare this uses-permission on AndroidManifest:



    <uses-permission android:name="android.permission.WAKE_LOCK" />


    Create a global field for WakeLock using static Android.OS.PowerManager;;



    private WakeLock wakeLock;


    And in your OnResume:



    PowerManager powerManager = (PowerManager)this.GetSystemService(Context.PowerService);
    WakeLock wakeLock = powerManager.NewWakeLock(WakeLockFlags.Full, "My Lock");
    wakeLock.Acquire();


    Just remember to release this lock when your application is paused or destroyed by doing this:



    wakeLock.Release();


    Usually, it's suggested to call the acquire method inside the onResume() of your activity and the release method in onPause(). This way we guarantee that our application still performs well in the case of being paused or resumed.



    Goodluck revert in case of queries






    share|improve this answer
























    • I saw an article about using xamarin essentials i dont know if that solution is implemented in all my views or do I have to implement it one by one

      – Lawrence Agulto
      Nov 29 '18 at 6:37











    • the article is here : docs.microsoft.com/en-us/xamarin/essentials/…

      – Lawrence Agulto
      Nov 29 '18 at 6:38











    • You have to implement it in just one, MainActivity for Android and Appdelegate for iOS!! Are you sure you read the answer?

      – G.hakim
      Nov 29 '18 at 6:44













    • I mean the code in the article do I have to implement it once or one by one?

      – Lawrence Agulto
      Nov 29 '18 at 6:47











    • I am not sure i have not used this API of Xamarin Essentials but i have posted the answer of what i have used and what works well for me

      – G.hakim
      Nov 29 '18 at 6:48
















    1














    For iOS, you need to override the DidFinishLaunchingWithOptions method in your Appdelegate class:



     public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
    {
    UIApplication.SharedApplication.IdleTimerDisabled = true;
    ....
    }


    For Android, you need to do the following things in your MainActivity for it:



    you have to declare this uses-permission on AndroidManifest:



    <uses-permission android:name="android.permission.WAKE_LOCK" />


    Create a global field for WakeLock using static Android.OS.PowerManager;;



    private WakeLock wakeLock;


    And in your OnResume:



    PowerManager powerManager = (PowerManager)this.GetSystemService(Context.PowerService);
    WakeLock wakeLock = powerManager.NewWakeLock(WakeLockFlags.Full, "My Lock");
    wakeLock.Acquire();


    Just remember to release this lock when your application is paused or destroyed by doing this:



    wakeLock.Release();


    Usually, it's suggested to call the acquire method inside the onResume() of your activity and the release method in onPause(). This way we guarantee that our application still performs well in the case of being paused or resumed.



    Goodluck revert in case of queries






    share|improve this answer
























    • I saw an article about using xamarin essentials i dont know if that solution is implemented in all my views or do I have to implement it one by one

      – Lawrence Agulto
      Nov 29 '18 at 6:37











    • the article is here : docs.microsoft.com/en-us/xamarin/essentials/…

      – Lawrence Agulto
      Nov 29 '18 at 6:38











    • You have to implement it in just one, MainActivity for Android and Appdelegate for iOS!! Are you sure you read the answer?

      – G.hakim
      Nov 29 '18 at 6:44













    • I mean the code in the article do I have to implement it once or one by one?

      – Lawrence Agulto
      Nov 29 '18 at 6:47











    • I am not sure i have not used this API of Xamarin Essentials but i have posted the answer of what i have used and what works well for me

      – G.hakim
      Nov 29 '18 at 6:48














    1












    1








    1







    For iOS, you need to override the DidFinishLaunchingWithOptions method in your Appdelegate class:



     public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
    {
    UIApplication.SharedApplication.IdleTimerDisabled = true;
    ....
    }


    For Android, you need to do the following things in your MainActivity for it:



    you have to declare this uses-permission on AndroidManifest:



    <uses-permission android:name="android.permission.WAKE_LOCK" />


    Create a global field for WakeLock using static Android.OS.PowerManager;;



    private WakeLock wakeLock;


    And in your OnResume:



    PowerManager powerManager = (PowerManager)this.GetSystemService(Context.PowerService);
    WakeLock wakeLock = powerManager.NewWakeLock(WakeLockFlags.Full, "My Lock");
    wakeLock.Acquire();


    Just remember to release this lock when your application is paused or destroyed by doing this:



    wakeLock.Release();


    Usually, it's suggested to call the acquire method inside the onResume() of your activity and the release method in onPause(). This way we guarantee that our application still performs well in the case of being paused or resumed.



    Goodluck revert in case of queries






    share|improve this answer













    For iOS, you need to override the DidFinishLaunchingWithOptions method in your Appdelegate class:



     public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
    {
    UIApplication.SharedApplication.IdleTimerDisabled = true;
    ....
    }


    For Android, you need to do the following things in your MainActivity for it:



    you have to declare this uses-permission on AndroidManifest:



    <uses-permission android:name="android.permission.WAKE_LOCK" />


    Create a global field for WakeLock using static Android.OS.PowerManager;;



    private WakeLock wakeLock;


    And in your OnResume:



    PowerManager powerManager = (PowerManager)this.GetSystemService(Context.PowerService);
    WakeLock wakeLock = powerManager.NewWakeLock(WakeLockFlags.Full, "My Lock");
    wakeLock.Acquire();


    Just remember to release this lock when your application is paused or destroyed by doing this:



    wakeLock.Release();


    Usually, it's suggested to call the acquire method inside the onResume() of your activity and the release method in onPause(). This way we guarantee that our application still performs well in the case of being paused or resumed.



    Goodluck revert in case of queries







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 29 '18 at 6:32









    G.hakimG.hakim

    5,32711136




    5,32711136













    • I saw an article about using xamarin essentials i dont know if that solution is implemented in all my views or do I have to implement it one by one

      – Lawrence Agulto
      Nov 29 '18 at 6:37











    • the article is here : docs.microsoft.com/en-us/xamarin/essentials/…

      – Lawrence Agulto
      Nov 29 '18 at 6:38











    • You have to implement it in just one, MainActivity for Android and Appdelegate for iOS!! Are you sure you read the answer?

      – G.hakim
      Nov 29 '18 at 6:44













    • I mean the code in the article do I have to implement it once or one by one?

      – Lawrence Agulto
      Nov 29 '18 at 6:47











    • I am not sure i have not used this API of Xamarin Essentials but i have posted the answer of what i have used and what works well for me

      – G.hakim
      Nov 29 '18 at 6:48



















    • I saw an article about using xamarin essentials i dont know if that solution is implemented in all my views or do I have to implement it one by one

      – Lawrence Agulto
      Nov 29 '18 at 6:37











    • the article is here : docs.microsoft.com/en-us/xamarin/essentials/…

      – Lawrence Agulto
      Nov 29 '18 at 6:38











    • You have to implement it in just one, MainActivity for Android and Appdelegate for iOS!! Are you sure you read the answer?

      – G.hakim
      Nov 29 '18 at 6:44













    • I mean the code in the article do I have to implement it once or one by one?

      – Lawrence Agulto
      Nov 29 '18 at 6:47











    • I am not sure i have not used this API of Xamarin Essentials but i have posted the answer of what i have used and what works well for me

      – G.hakim
      Nov 29 '18 at 6:48

















    I saw an article about using xamarin essentials i dont know if that solution is implemented in all my views or do I have to implement it one by one

    – Lawrence Agulto
    Nov 29 '18 at 6:37





    I saw an article about using xamarin essentials i dont know if that solution is implemented in all my views or do I have to implement it one by one

    – Lawrence Agulto
    Nov 29 '18 at 6:37













    the article is here : docs.microsoft.com/en-us/xamarin/essentials/…

    – Lawrence Agulto
    Nov 29 '18 at 6:38





    the article is here : docs.microsoft.com/en-us/xamarin/essentials/…

    – Lawrence Agulto
    Nov 29 '18 at 6:38













    You have to implement it in just one, MainActivity for Android and Appdelegate for iOS!! Are you sure you read the answer?

    – G.hakim
    Nov 29 '18 at 6:44







    You have to implement it in just one, MainActivity for Android and Appdelegate for iOS!! Are you sure you read the answer?

    – G.hakim
    Nov 29 '18 at 6:44















    I mean the code in the article do I have to implement it once or one by one?

    – Lawrence Agulto
    Nov 29 '18 at 6:47





    I mean the code in the article do I have to implement it once or one by one?

    – Lawrence Agulto
    Nov 29 '18 at 6:47













    I am not sure i have not used this API of Xamarin Essentials but i have posted the answer of what i have used and what works well for me

    – G.hakim
    Nov 29 '18 at 6:48





    I am not sure i have not used this API of Xamarin Essentials but i have posted the answer of what i have used and what works well for me

    – G.hakim
    Nov 29 '18 at 6:48













    1














    Xamarin.Forms:



    //show something important, do not sleep
    DependencyService.Get<INativeTasks>().ExecuteTask("cannotSleep");

    //can put in OnDisappearing event
    DependencyService.Get<INativeTasks>().ExecuteTask("canSleep");


    Native tasks helper:



     public interface INativeTasks
    {
    ...
    void ExecuteTask(string task, object param=null);
    ...
    }


    Android:



    Global variables and other..



    public class DroidCore
    {
    private static DroidCore instance;
    public static DroidCore Current
    {
    get { return instance ?? (instance = new DroidCore()); }
    }

    public static Window MainWindow { get; set; }
    ...
    }


    MainActivity.cs



    protected override void OnCreate(Bundle bundle)
    {
    ...
    DroidCore.Current.MainView = this.Window.DecorView;
    ...
    }


    Native helpers:



    public class NativeTasks : INativeTasks
    {
    public void ExecuteTask(string task, object param = null)
    {
    switch (task)
    {

    ... //any native stuff you can imagine

    case "cannotSleep":
    DroidCore.MainWindow.AddFlags(WindowManagerFlags.KeepScreenOn);
    break;

    case "canSleep":
    DroidCore.MainWindow.ClearFlags(WindowManagerFlags.KeepScreenOn);
    break;
    }
    }
    }


    iOS:



    Native helpers:



    public class NativeTasks : INativeTasks
    {
    public void ExecuteTask(string task, object param = null)
    {
    switch (task)
    {

    ... //any native stuff you can imagine

    case "cannotSleep":
    UIApplication.SharedApplication.IdleTimerDisabled = true;
    break;

    case "canSleep":
    UIApplication.SharedApplication.IdleTimerDisabled = false;
    break;
    }
    }
    }





    share|improve this answer
























    • I am curious about something here! Why write a renderer for something like this? if you want an application wide disabled auto-lock like the questioner has asked for just curious!

      – G.hakim
      Nov 29 '18 at 8:27













    • Native tasks are just for everything you can't do in forms. Have many other tasks implemented, posted the part you were asking for. Now when you hit the Forms wall with any native problem just put your native code inside INativeTasks and fly. :)

      – Nick Kovalsky
      Nov 29 '18 at 8:39
















    1














    Xamarin.Forms:



    //show something important, do not sleep
    DependencyService.Get<INativeTasks>().ExecuteTask("cannotSleep");

    //can put in OnDisappearing event
    DependencyService.Get<INativeTasks>().ExecuteTask("canSleep");


    Native tasks helper:



     public interface INativeTasks
    {
    ...
    void ExecuteTask(string task, object param=null);
    ...
    }


    Android:



    Global variables and other..



    public class DroidCore
    {
    private static DroidCore instance;
    public static DroidCore Current
    {
    get { return instance ?? (instance = new DroidCore()); }
    }

    public static Window MainWindow { get; set; }
    ...
    }


    MainActivity.cs



    protected override void OnCreate(Bundle bundle)
    {
    ...
    DroidCore.Current.MainView = this.Window.DecorView;
    ...
    }


    Native helpers:



    public class NativeTasks : INativeTasks
    {
    public void ExecuteTask(string task, object param = null)
    {
    switch (task)
    {

    ... //any native stuff you can imagine

    case "cannotSleep":
    DroidCore.MainWindow.AddFlags(WindowManagerFlags.KeepScreenOn);
    break;

    case "canSleep":
    DroidCore.MainWindow.ClearFlags(WindowManagerFlags.KeepScreenOn);
    break;
    }
    }
    }


    iOS:



    Native helpers:



    public class NativeTasks : INativeTasks
    {
    public void ExecuteTask(string task, object param = null)
    {
    switch (task)
    {

    ... //any native stuff you can imagine

    case "cannotSleep":
    UIApplication.SharedApplication.IdleTimerDisabled = true;
    break;

    case "canSleep":
    UIApplication.SharedApplication.IdleTimerDisabled = false;
    break;
    }
    }
    }





    share|improve this answer
























    • I am curious about something here! Why write a renderer for something like this? if you want an application wide disabled auto-lock like the questioner has asked for just curious!

      – G.hakim
      Nov 29 '18 at 8:27













    • Native tasks are just for everything you can't do in forms. Have many other tasks implemented, posted the part you were asking for. Now when you hit the Forms wall with any native problem just put your native code inside INativeTasks and fly. :)

      – Nick Kovalsky
      Nov 29 '18 at 8:39














    1












    1








    1







    Xamarin.Forms:



    //show something important, do not sleep
    DependencyService.Get<INativeTasks>().ExecuteTask("cannotSleep");

    //can put in OnDisappearing event
    DependencyService.Get<INativeTasks>().ExecuteTask("canSleep");


    Native tasks helper:



     public interface INativeTasks
    {
    ...
    void ExecuteTask(string task, object param=null);
    ...
    }


    Android:



    Global variables and other..



    public class DroidCore
    {
    private static DroidCore instance;
    public static DroidCore Current
    {
    get { return instance ?? (instance = new DroidCore()); }
    }

    public static Window MainWindow { get; set; }
    ...
    }


    MainActivity.cs



    protected override void OnCreate(Bundle bundle)
    {
    ...
    DroidCore.Current.MainView = this.Window.DecorView;
    ...
    }


    Native helpers:



    public class NativeTasks : INativeTasks
    {
    public void ExecuteTask(string task, object param = null)
    {
    switch (task)
    {

    ... //any native stuff you can imagine

    case "cannotSleep":
    DroidCore.MainWindow.AddFlags(WindowManagerFlags.KeepScreenOn);
    break;

    case "canSleep":
    DroidCore.MainWindow.ClearFlags(WindowManagerFlags.KeepScreenOn);
    break;
    }
    }
    }


    iOS:



    Native helpers:



    public class NativeTasks : INativeTasks
    {
    public void ExecuteTask(string task, object param = null)
    {
    switch (task)
    {

    ... //any native stuff you can imagine

    case "cannotSleep":
    UIApplication.SharedApplication.IdleTimerDisabled = true;
    break;

    case "canSleep":
    UIApplication.SharedApplication.IdleTimerDisabled = false;
    break;
    }
    }
    }





    share|improve this answer













    Xamarin.Forms:



    //show something important, do not sleep
    DependencyService.Get<INativeTasks>().ExecuteTask("cannotSleep");

    //can put in OnDisappearing event
    DependencyService.Get<INativeTasks>().ExecuteTask("canSleep");


    Native tasks helper:



     public interface INativeTasks
    {
    ...
    void ExecuteTask(string task, object param=null);
    ...
    }


    Android:



    Global variables and other..



    public class DroidCore
    {
    private static DroidCore instance;
    public static DroidCore Current
    {
    get { return instance ?? (instance = new DroidCore()); }
    }

    public static Window MainWindow { get; set; }
    ...
    }


    MainActivity.cs



    protected override void OnCreate(Bundle bundle)
    {
    ...
    DroidCore.Current.MainView = this.Window.DecorView;
    ...
    }


    Native helpers:



    public class NativeTasks : INativeTasks
    {
    public void ExecuteTask(string task, object param = null)
    {
    switch (task)
    {

    ... //any native stuff you can imagine

    case "cannotSleep":
    DroidCore.MainWindow.AddFlags(WindowManagerFlags.KeepScreenOn);
    break;

    case "canSleep":
    DroidCore.MainWindow.ClearFlags(WindowManagerFlags.KeepScreenOn);
    break;
    }
    }
    }


    iOS:



    Native helpers:



    public class NativeTasks : INativeTasks
    {
    public void ExecuteTask(string task, object param = null)
    {
    switch (task)
    {

    ... //any native stuff you can imagine

    case "cannotSleep":
    UIApplication.SharedApplication.IdleTimerDisabled = true;
    break;

    case "canSleep":
    UIApplication.SharedApplication.IdleTimerDisabled = false;
    break;
    }
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 29 '18 at 8:21









    Nick KovalskyNick Kovalsky

    1,371720




    1,371720













    • I am curious about something here! Why write a renderer for something like this? if you want an application wide disabled auto-lock like the questioner has asked for just curious!

      – G.hakim
      Nov 29 '18 at 8:27













    • Native tasks are just for everything you can't do in forms. Have many other tasks implemented, posted the part you were asking for. Now when you hit the Forms wall with any native problem just put your native code inside INativeTasks and fly. :)

      – Nick Kovalsky
      Nov 29 '18 at 8:39



















    • I am curious about something here! Why write a renderer for something like this? if you want an application wide disabled auto-lock like the questioner has asked for just curious!

      – G.hakim
      Nov 29 '18 at 8:27













    • Native tasks are just for everything you can't do in forms. Have many other tasks implemented, posted the part you were asking for. Now when you hit the Forms wall with any native problem just put your native code inside INativeTasks and fly. :)

      – Nick Kovalsky
      Nov 29 '18 at 8:39

















    I am curious about something here! Why write a renderer for something like this? if you want an application wide disabled auto-lock like the questioner has asked for just curious!

    – G.hakim
    Nov 29 '18 at 8:27







    I am curious about something here! Why write a renderer for something like this? if you want an application wide disabled auto-lock like the questioner has asked for just curious!

    – G.hakim
    Nov 29 '18 at 8:27















    Native tasks are just for everything you can't do in forms. Have many other tasks implemented, posted the part you were asking for. Now when you hit the Forms wall with any native problem just put your native code inside INativeTasks and fly. :)

    – Nick Kovalsky
    Nov 29 '18 at 8:39





    Native tasks are just for everything you can't do in forms. Have many other tasks implemented, posted the part you were asking for. Now when you hit the Forms wall with any native problem just put your native code inside INativeTasks and fly. :)

    – Nick Kovalsky
    Nov 29 '18 at 8:39











    0














    We can achieve it by using Xamarin.Essentials plugin. Install it on solution level(While installing select include prerelease checkbox).



    In your MainPage write this code



    public partial class MainPage : ContentPage
    {
    public MainPage()
    {
    InitializeComponent();
    ToggleScreenLock();
    }
    public void ToggleScreenLock()
    {
    if (!ScreenLock.IsActive)
    ScreenLock.RequestActive();
    else
    ScreenLock.RequestRelease();
    }
    }


    In Android project MainActivity add this line



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


    before calling LoadApplication(new App());. For more information visit Microsoft docs.



    This is working throughout the app. To install plugin refer below screenshot -



    enter image description here






    share|improve this answer






























      0














      We can achieve it by using Xamarin.Essentials plugin. Install it on solution level(While installing select include prerelease checkbox).



      In your MainPage write this code



      public partial class MainPage : ContentPage
      {
      public MainPage()
      {
      InitializeComponent();
      ToggleScreenLock();
      }
      public void ToggleScreenLock()
      {
      if (!ScreenLock.IsActive)
      ScreenLock.RequestActive();
      else
      ScreenLock.RequestRelease();
      }
      }


      In Android project MainActivity add this line



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


      before calling LoadApplication(new App());. For more information visit Microsoft docs.



      This is working throughout the app. To install plugin refer below screenshot -



      enter image description here






      share|improve this answer




























        0












        0








        0







        We can achieve it by using Xamarin.Essentials plugin. Install it on solution level(While installing select include prerelease checkbox).



        In your MainPage write this code



        public partial class MainPage : ContentPage
        {
        public MainPage()
        {
        InitializeComponent();
        ToggleScreenLock();
        }
        public void ToggleScreenLock()
        {
        if (!ScreenLock.IsActive)
        ScreenLock.RequestActive();
        else
        ScreenLock.RequestRelease();
        }
        }


        In Android project MainActivity add this line



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


        before calling LoadApplication(new App());. For more information visit Microsoft docs.



        This is working throughout the app. To install plugin refer below screenshot -



        enter image description here






        share|improve this answer















        We can achieve it by using Xamarin.Essentials plugin. Install it on solution level(While installing select include prerelease checkbox).



        In your MainPage write this code



        public partial class MainPage : ContentPage
        {
        public MainPage()
        {
        InitializeComponent();
        ToggleScreenLock();
        }
        public void ToggleScreenLock()
        {
        if (!ScreenLock.IsActive)
        ScreenLock.RequestActive();
        else
        ScreenLock.RequestRelease();
        }
        }


        In Android project MainActivity add this line



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


        before calling LoadApplication(new App());. For more information visit Microsoft docs.



        This is working throughout the app. To install plugin refer below screenshot -



        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 29 '18 at 8:22

























        answered Nov 29 '18 at 7:31









        ArvindrajaArvindraja

        2,94931132




        2,94931132






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53532836%2fxamarin-forms-disable-auto-lock-when-the-app-is-open%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            Idjassú

            count number of partitions of a set with n elements into k subsets