Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to keep the Android system from hibernation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to make Android system not sleep", the content is simple and easy to understand, organized clearly, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how to make Android system not sleep" this article bar.

Wake Lock is a lock mechanism that prevents the system from going to sleep as long as someone holds the lock.

Can be obtained by user mode programs and kernels. this lock can be timed out or not timed out.

Timed locks will unlock automatically after the time has elapsed. If there is no lock or timeout, the kernel will

It activates the dormancy mechanism to go into hibernation.

1. The kernel maintains two linked lists

1). linked list, active_wake_locks[WAKE_LOCK_TYPE_COUNT]

active_wake_locks[0] maintains a suspend lock.

active_wake_locks[1] maintains idle locks.

2). The list, inactive_locks, records all inactive locks.

2. Apply for lock process

For example, the PowerManagerService generation process under/sys/power/wake_lock.

1). PowerManager class

Android provides a ready-made android.os.PowerManager class, which provides newWakeLock(int flags, String tag) method to obtain the corresponding level of lock. The definition of this function frameworks/base/core/java/android/os/PowerManager.java below, applications will call when applying for wake_lock.

Examples:

PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);

PowerManager.WakeLock wl = pm.newWakeLock

(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");

wl.acquire();//request lock This will call acquireWakeLock() in PowerManagerService

wl.release(); //Release lock, show the release, if the requested lock is not here release system will not go to sleep.

2). frameworks layer

/frameworks/base/services/java/com/android/server/

PowerManagerService.java This class is used to manage all wakelock applications. For example, audio and video players, cameras and other applications of wakelock are managed through this class. static final String PARTIAL_NAME = "PowerManagerService"Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME); This function calls acquireWakeLock() in the Power class, where PARTIAL_NAME is passed to the underlying layer as a parameter.

/frameworks/base/core/java/android/os/Power.java

public static native void acquireWakeLock(int lock, String id);

Note: The method declared by native is not implemented in the Power class, and its implementation body is in frameworks/base/core/jni/android_os_Power.cpp, so calling the acquireWakeLock() method of the Power class will call the implementation method under JNI.

3).JNI layer implementation

Route: frameworks/base/core/jni/android_os_Power.cpp

static void acquireWakeLock(JNIEnv *env, jobject clazz, jint lock, jstring idObj)

{

const char *id = env->GetStringUTFChars(idObj, NULL);

acquire_wake_lock(lock, id);

env->ReleaseStringUTFChars(idObj, id);

}

Note: Acquire_wake_lock(lock, id) under hardware/libhardware_legacy/power/power.c is called in acquireWakeLock()

4). Interaction with kernel layer

The acquire_wake_lock(lock, id) function under power.c is as follows:

int acquire_wake_lock(int lock, const char* id)

{

return write(fd, id, strlen(id));

}

Note: fd is the file descriptor, which means "/sys/power/wake_lock" id is the parameter passed down from PowerManagerService class, i.e.: PARTIAL_NAME = "PowerManagerService" This is the place to interact with kernel layer through file system.

3. Examples

To keep android from going to sleep, add three lines of code to the kernel. Keeps this lock. The system doesn't go into hibernation. As follows:

static struct wake_lock wqf_charge_display_lock; //Declare a lock

wake_lock_init(&wqf_charge_display_lock, WAKE_LOCK_SUSPEND, "wqf_charge_display_lock"); //wqf charge_display_lock initializes the lock

wake_lock(&wqf_charge_display_lock);//wqf modify Keep this lock so that the system does not go to sleep

That's all for "How to keep Android from sleeping". Thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report