Program description:
This program will demo how to use wake locks in android.
Before seeing the demo program lets understand what is wake lock and sleep mode.
Major difference between phones and laptops (In terms of power):
Laptops mostly work on direct power through charger. Most of the times we carry power cables while using laptops.
Phones mostly work on battery power. Very rarely we carry phone chargers along with us while going out.
That means phones rely heavily on battery power, where as for laptops there is no such constraint.
Based on this assumption, we can come to a conclusion that phones should use battery very efficiently in order to increase battery back up time.
Meaning, programmers should write code to consume less battery power.
Sleep mode:
Based on above theory, android has done some major changes in the power manager driver of O.S layer. If user is not using the phone, then automatically LCD or screen of the phone will be turned off. With in fraction of seconds CPU will go to sleep mode. This is done in order to increase battery life in phones.
Note: If LCD of phone is turned off means, user is not using or not viewing at the phone. So when user is not using the phone, there is no point in keeping CPU running. That's why with in fraction of seconds CPU will be moved to sleep mode, and stops consuming battery power.
What components works when CPU is in sleep mode?
When CPU is in sleep mode, only Alarms & RIL (Radio interface layer) components works.
RIL : includes Calls, sms, mms, internet connection.
Apart from these components, nothing else will work.
Note: In android if we switch off the phone, all the alarms will be destroyed.
If CPU is in sleep mode, does my application stops working?
When CPU is in sleep mode, all the applications will be moved to suspended state. They will be resumed once LCD is turned on (which makes CPU to turn on).
So I can't run my application when CPU is in sleep mode?
You can request CPU to wake up by using wake locks, to run your application components.
Wake locks:
Wake locks are used to wake up the CPU from sleep mode, and perform some important tasks which are useful to user.
eg: I want to book tickets in IRCTC at late night 11pm. Mostly at 11pm user will be sleeping, so LCD will be turned off, which makes CPU to sleep mode. But still you can wake up CPU by using wake locks and book tickets in the back ground and then release the wake locks.
Types of wake locks:
There are 4 types of wake locks available in android.
1. PARTIAL_WAKE_LOCK : by using this only CPU will be turned on. eg: use partial wake lock, to play songs while in the journey.
2. SCREEN_DIM_WAKE_LOCK : by using this CPU, and LCD will turned on. But LCD will be in dim mode. eg: use this wake lock to see the time.
3. SCREEN_BRIGHT_WAKE_LOCK : by using this CPU, and LCD will be turned on. LCD will be in full bright mode. eg: use this wake lock to watch a movie.
4. FULL_WAKE_LOCK : by using this CPU, LCD, and Keypad will be on. eg: use this wake lock to play a game.
Below is a sample demo program on how to use PARTIAL_WAKE_LOCK.
Note: to use wake locks, programmer has to take android.permission.WAKE_LOCK.
else program will crash with security exception.
First Activity:
This program will demo how to use wake locks in android.
Before seeing the demo program lets understand what is wake lock and sleep mode.
Major difference between phones and laptops (In terms of power):
Laptops mostly work on direct power through charger. Most of the times we carry power cables while using laptops.
Phones mostly work on battery power. Very rarely we carry phone chargers along with us while going out.
That means phones rely heavily on battery power, where as for laptops there is no such constraint.
Based on this assumption, we can come to a conclusion that phones should use battery very efficiently in order to increase battery back up time.
Meaning, programmers should write code to consume less battery power.
Sleep mode:
Based on above theory, android has done some major changes in the power manager driver of O.S layer. If user is not using the phone, then automatically LCD or screen of the phone will be turned off. With in fraction of seconds CPU will go to sleep mode. This is done in order to increase battery life in phones.
Note: If LCD of phone is turned off means, user is not using or not viewing at the phone. So when user is not using the phone, there is no point in keeping CPU running. That's why with in fraction of seconds CPU will be moved to sleep mode, and stops consuming battery power.
What components works when CPU is in sleep mode?
When CPU is in sleep mode, only Alarms & RIL (Radio interface layer) components works.
RIL : includes Calls, sms, mms, internet connection.
Apart from these components, nothing else will work.
Note: In android if we switch off the phone, all the alarms will be destroyed.
If CPU is in sleep mode, does my application stops working?
When CPU is in sleep mode, all the applications will be moved to suspended state. They will be resumed once LCD is turned on (which makes CPU to turn on).
So I can't run my application when CPU is in sleep mode?
You can request CPU to wake up by using wake locks, to run your application components.
Wake locks:
Wake locks are used to wake up the CPU from sleep mode, and perform some important tasks which are useful to user.
eg: I want to book tickets in IRCTC at late night 11pm. Mostly at 11pm user will be sleeping, so LCD will be turned off, which makes CPU to sleep mode. But still you can wake up CPU by using wake locks and book tickets in the back ground and then release the wake locks.
Types of wake locks:
There are 4 types of wake locks available in android.
1. PARTIAL_WAKE_LOCK : by using this only CPU will be turned on. eg: use partial wake lock, to play songs while in the journey.
2. SCREEN_DIM_WAKE_LOCK : by using this CPU, and LCD will turned on. But LCD will be in dim mode. eg: use this wake lock to see the time.
3. SCREEN_BRIGHT_WAKE_LOCK : by using this CPU, and LCD will be turned on. LCD will be in full bright mode. eg: use this wake lock to watch a movie.
4. FULL_WAKE_LOCK : by using this CPU, LCD, and Keypad will be on. eg: use this wake lock to play a game.
Below is a sample demo program on how to use PARTIAL_WAKE_LOCK.
Note: to use wake locks, programmer has to take android.permission.WAKE_LOCK.
else program will crash with security exception.
First Activity:
package com.techpalle.b15_testpower; import android.os.Bundle; import android.os.PowerManager; import android.app.Activity; import android.content.Context; import android.view.Menu; public class MainActivity extends Activity { PowerManager pm; PowerManager.WakeLock wl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "satish"); wl.acquire(); //... wl.release(); } }
xml file for first activity
File name : activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</RelativeLayout>
Manifest file
File name : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.techpalle.b15_testpower"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.techpalle.b15_testpower.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>
Download complete code : Click to download
Android interview questions on Power manager and wake locks:
What is sleep mode? What will happen to CPU when LCD is turned off?
What are the two components that runs in Sleep mode also?
I would like to thank you for the efforts you have made in writing this article, Its good and Informative.
ReplyDeleteandroid online training