|1.0 (2020-03-07)||Initial version|
|1.1 (2020-07-16)||Methods added (section battery optimization, see below):
|1.2 (2020-08-05)||Release() had no effect.|
|1.3 (2020-08-08)||Calling Release multiple times led to an exception.|
|1.4 (2021-01-26)||WifiLock added.|
|1.5 (2021-03-21)||- Properties Version and VersionSDK
- SDK version check for functions for setting the battery optimization.
- Fixed bug with RequestIgnoreBatteryOptimization.
|1.6 (2021-04-09)||Method IsRunningInCompanion added.
Note: The examples are not affected and still contain version 1.5 of the extension.
|1.7 (2021-07-20)||Properties OnAfterRelease, AcquireCausesWakeup and Methods AquireFullWakeLock, EnableShowOnLockScreen added.|
For some projects it is necessary to prevent the associated app from being deactivated by the operating system. In early Android versions it was possible to set a WakeLock. This ensured that the app was not switched off by the operating system.
With version 6.0 (Marshmallow) Android introduced Doze mode to optimize battery life. If no app is actively used, this function gradually switches down everything (display, CPU, WiFi, etc.). The WakeLock function has also been partially disabled.
The MIT App Inventor has no functions to prevent the shutdown. You can use the Clock component as a timer and also set the events to be triggered in the background (TimerAlwaysFires). With my smartphone with the Android version Oreo 8.1, however, what could be done within a timer event is very limited. Network functions, for example, are not available
With the extension described here, you can ensure that the CPU is not switched off and the app remains active for a long period of time.
A lot of useful background information can be found in the ZEBRA Developer Portal: Keeping your Android application running when the device wants to sleep.
The UrsAI2WakeLock ZIP archive for download. The archive contains the source code, the compiled binary for uploading to the App Inventor and a sample application.
In principle, there are three use cases:
Android offers the following possible solutions for these cases:
Android has changed its power management policy many times. So you should definitely test whether the information given here applies to your own project.
A Wakelock does not protect against the network connection being cut after a certain period of time. The WifiLock enables an application to keep the WLAN connection active. Typically, the WiFi hardware is turned off when the user has not used the device for a long time. If a WifiLock is set, the hardware keeps switched orunning until the lock is released.
In addition to the methods for setting up WakeLocks, Android offers three functions to influence the battery optimization by code. You also can switch off the battery optimization via the settings (see below Settings for battery optimization).
The Android documentation refers to a whitelist. With my (German) Xiaomi in the Redmi 5 (German version) I could not verify this. Therefore I refer to the Android documentation.
The RequestIgnoreBatteryOptimization method asks the user to switch off battery optimization for the app. After calling the method, this request appears:
|Sorry, no English screenshot. Translated:
Should the app always run in the background?
Allowing "Urs WakeLock Test" to always run in the background can reduce battery life.
You can change this later under "Settings"> Apps & notifications ".
The IsIgnoringBatteryOptimizations property can be used to query whether the app is on the whitelist. OpenBatteryOptimizationSettings opens a dialog that can be used to manually maintain the whitelist.
Android allows the energy-saving mode to be switched off specifically for individual apps. Here are the instructions for my smartphone (Xiaomi Redmi 5 Plus) with Android Oreo 8.1:
Sorry, no English instructions :-(
A small sample app shows how to use the extension (see download).
A running clock is displayed in the upper part. In order to be able to control an app running in the background, a broadcast packet is sent every ten seconds via UDP with the current time.
The various WakeLocks can be set using the buttons. The checkbox shows whether a WakeLock is active (evaluation of the IsActive property). In the lower part is shown which type of WakeLock is active (evaluation of the WakeLockType property)
I tested the following scenarios with the example app.
|Lock type||power saving mode|
|none||on (this is the standard after installing the app)||This is the default. If it is not operated, the display is darkened after about ten seconds and switched off after another five seconds. The app continues to run for a while, sometimes for a few minutes. However, this time is not fixed and was different for each probe.|
|none||off (according to the instructions under "Settings for battery optimization")||The screen turns off, but the app continues broadcasting. The time intervals are not as designated, the UDP packets are sent irregularly.|
As long as the app is in the foreground, it remains active; the display may be darkened.
The UDP packets are sent at the specified times.
|PARTIAL_WAKE_LOCK||on||No effect, the app behaves like the default|
If thr the app is in the foreground, the UDP packets are sent regularly.
|Power saving mode:||on||on||on||on||off||off||off||off|
then very irregular
For developing own extensions I gathered some tips: AI2 FAQ: Develop Extensions.