Updated 27 April 2023
In today’s article we will learn about wakelock in flutter. After the user has been inactive on a mobile device for a predetermined period of time, the device goes to sleep. It is carried out to preserve the device’s power. The CPU of the device ceases operating when it goes to sleep. But occasionally the software needs to keep the device awake because it has a crucial background activity running.
Wake lock is a feature of the PowerManager system service that enables your application to manage the device’s power state.
Your strategy will rely on what your app requires. To reduce your app’s impact on system resources, you should utilise the most lightweight approach feasible, according to popular wisdom. The sections that follow explain how to manage situations where your app’s requirements conflict with the device’s standard sleep behaviour.
Use wake locks, a PowerManager system service feature, if you need to keep the CPU active so that you can do some task before the device goes to sleep. Wake locks provide your programme control over the host device’s power state.
Check our Flutter app development company page
1. Project Setup:
Create a new flutter project and add latest wakelock version under dependencies in pubspec.yaml file of your project as following example and run flutter pub get command to install the package in your project.
2. Import Package:
Import wakelock using following line in the class you need to implement this functionality.
1 |
import 'package:wakelock/wakelock.dart'; |
3. Implementation:
Wake lock class has three methods to use wakelock functionality as follows:
a) Wakelock.enable(), b) Wakelock.disable(), Wakelock.toggle(on:true);
1 2 3 4 5 6 7 8 9 10 11 |
OutlinedButton( onPressed: () { // The following code will enable the wake lock on the device // using the wakelock plugin. setState(() { Wakelock.enable(); // You could also use Wakelock.toggle(on: true); }); }, child: const Text('enable wakelock'), ), |
1 2 3 4 5 6 7 8 9 10 11 |
OutlinedButton( onPressed: () { // The following code will disable the wake lock on the device // using the wake lock plugin. setState(() { Wakelock.disable(); // You could also use Wakelock.toggle(on: false); }); }, child: const Text('disable wakelock'), ), |
In this article, we have learned about Wake lock with a simple mobikul demo flutter app using wakelock: ^0.6.2 plugin.
Thanks for reading the blog, for more such informative articles related to latest tech trend please visit our mobikul blog site.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.