Prevent Mobile Going Into Idle Mode


This short tutorial will be about how to prevent a mobile phone or device from turning of the screen, and going into idle mode to save power, when the user do not touch the mobile screen for some time. You might like to prevent this default behaviour when the user plays a game using the accelerometer in the phone or device.

You need to know how to create a Flex Mobile Project or ActionScript Mobile Project in Flash Builder 4.5 to follow along in this tutorial. You find my guide on how to create a Flex Mobile Project here and my guide on how to create an ActionScript Mobile Project here.

Add the following line of ActionScript code to you application to prevent the screen on the mobile phone from turning of and going to sleep.

NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;

For this to work on a Google Android mobile phone or device you also need to set permissions in the manifest file. This can be done in two ways.

1. The easiest way to do this is in the new project wizard when you create your mobile project. You do it at the second step called Mobile Settings. Locate the tab Permissions and select the Google Android platform in the drop down menu. Then check the checkbox next to DISABLE_KEYGARD, WAKE_LOCK. Checking this permission will allow you to keep the phone awake and prevent it from going to sleep when the user is inactive with the screen. This checkbox can only be ticked / unticked while creating your mobile project. You have to use the second method to set the permissions if you forgot to check it while creating the mobile project.

2. The second way to set the permissions involves opening the Adobe AIR Application Descriptor File. It will be named the same as your project with -app.xml at the end and will be located in the root of your source folder, default name is src, open the file and go to the end of it and locate manifestAddition and

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

Uncomment these two lines by removing <!-- --> from the XML code so that the lines read

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

This is all you have to do. Compile and run your app on your mobile phone or device to test if it really prevents the phone from going to sleep / idle mode.