Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/molinch/Xam.Plugins.ManageSleep

Manage auto sleep / auto lock in all platforms
https://github.com/molinch/Xam.Plugins.ManageSleep

Last synced: 3 months ago
JSON representation

Manage auto sleep / auto lock in all platforms

Awesome Lists containing this project

README

        

Xam.Plugins.ManageSleep plugin for Xamarin
==========================================

Manage auto sleep / auto lock in all platforms. This is useful when dealing with long running processes.

NuGet package is available here: https://www.nuget.org/packages/Xam.Plugins.ManageSleep/

### Usage
**Android requires `WAKE_LOCK` permission, see below.**

This should fit most scenarios. Instanciate SleepMode and use one of its method:

```C#
///
/// Executes the given action without having the device going to sleep.
///
/// Action to execute.
void DoWithoutSleep(Action action);

///
/// Awaits the given task without having the device going to sleep.
///
/// A task to await.
/// Task to run.
Task DoWithoutSleepAsync(Task action);
```

Example:
```C#
var sleepMode = new SleepMode();
sleepMode.DoWithoutSleep(() => {
//long running operation...
});
```
### Android permissions

Android requires that the app manifest includes the `WAKE_LOCK` permission.

https://developer.android.com/reference/android/Manifest.permission.html#WAKE_LOCK

Include this permission via your IDE or including this XML:

```

```

### Advanced usage
If you need a fine-grained control of the auto sleep. Then you can use the following method:
```C#
///
/// Activates or desactivates the auto sleep mode. True to activate it (default), False to deactivate it.
/// Use with caution: if you deactivated auto sleep you will need to reactivate it.
/// DoWithoutSleep and DoWithoutSleepAsync methods are preferred since they automatically resume auto sleep.
///
/// If set to true activates auto sleep mode.
void ActivateAutoSleepMode(bool activateAutoSleepMode);
```

Therefore be careful when using it: if you deactivated auto sleep then you should reactivate it. Furthermore it has to be done using the same instance of SleepMode.

If you use a framework like MvvmCross then it is easier: you will typically have one instance of SleepMode registered as a singleton for the ISleepMode interface.