Android 休眠与唤醒的实现源码分析
Posted 王睿丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 休眠与唤醒的实现源码分析相关的知识,希望对你有一定的参考价值。
1、代码实现
🔺说明:
应用层:
android:sharedUserId="android.uid.system" 除了声明系统权限,还需要系统签名APK
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DEVICE_POWER" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
框架层:因为PowerManagerl类的reboot()、shutdown不对外公开,所以我们在Android Framework层进行实现!
通过JNI技术让上层代码与底层代码产生联系,就可以实现休眠与唤醒!
1.1、休眠
public static void sleep(Context context) {
IPowerManager mPowerManager = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
try{
mPowerManager.goToSleep(SystemClock.uptimeMillis()+1,0,0);
}catch (Exception e){
Log.d("SleepAndWakeLockControl","休眠失败");
}
}
1.2、唤醒
public static void wake(Context context){
IPowerManager mPowerManager = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
try {
mPowerManager.wakeUp(SystemClock.uptimeMillis()+1,0,"wakeUp",context.getOpPackageName());
}catch (Exception e){
Log.d("SleepAndWakeLockControl","唤醒失败");
}
}
2、源码分析
2.1、休眠
/**
* Forces the device to go to sleep.
* <p>
* Overrides all the wake locks that are held.
* This is what happens when the power key is pressed to turn off the screen.
* </p><p>
* Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
* </p>
*
* @param time The time when the request to go to sleep was issued, in the
* {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
* order the go to sleep request with other power management functions. It should be set
* to the timestamp of the input event that caused the request to go to sleep.
* @param reason The reason the device is going to sleep.
* @param flags Optional flags to apply when going to sleep.
*
* @see #userActivity
* @see #wakeUp
*
* @hide Requires signature permission.
*/
@UnsupportedAppUsage
public void goToSleep(long time, int reason, int flags) {
try {
mService.goToSleep(time, reason, flags);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
2.2、唤醒
/**
* Forces the device to wake up from sleep.
* <p>
* If the device is currently asleep, wakes it up, otherwise does nothing.
* This is what happens when the power key is pressed to turn on the screen.
* </p><p>
* Requires the {@link android.Manifest.permission#DEVICE_POWER} permission.
* </p>
*
* @param time The time when the request to wake up was issued, in the
* {@link SystemClock#uptimeMillis()} time base. This timestamp is used to correctly
* order the wake up request with other power management functions. It should be set
* to the timestamp of the input event that caused the request to wake up.
*
* @param reason The reason for the wake up.
*
* @param details A free form string to explain the specific details behind the wake up for
* debugging purposes.
*
* @see #userActivity
* @see #goToSleep
* @hide
*/
public void wakeUp(long time, @WakeReason int reason, String details) {
try {
mService.wakeUp(time, reason, details, mContext.getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
3、开发时坑点
1、需要系统签名的APK,大家应该都知道,我主要讲第二个
2、无论是toSleep()、wakeUp()它的time参数都要用:
SystemClock.uptimeMillis()+几都行
SystemClock.uptimeMillis()参数主要表示:从开机到现在的毫秒数(手机睡眠的时间不包括在内);
如果不使用这个参数,直接填1*1000或者其他则功能只能使用一次,第二次就会失灵。
以上是关于Android 休眠与唤醒的实现源码分析的主要内容,如果未能解决你的问题,请参考以下文章