如何修改源码android 实现按键唤醒屏幕

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修改源码android 实现按键唤醒屏幕相关的知识,希望对你有一定的参考价值。

尽管手机的电源按键普遍比较耐用,不过使用久了也容易导致电源按键失灵。手机电源键失灵怎么办?维修更换吗?不过这需要花费一些银子。如果不想花钱怎么办?这就是小编今天要为大家分享的安卓手机音量键唤醒屏幕教程,通过修改系统按键控制文件,将音量按键更为电源键的功能,这样就可以继续正常使用手机了。 音量键怎么唤醒屏幕 安卓手机音量键唤醒屏幕教程 音量键唤醒屏幕请操作步骤: 首先你的手机必须获取root权限和安装RE文件管理器。安卓手机ROOT相信大家都懂,很多安卓手机助手都支持一键ROOT。ROOT并安装RE管理器后,接下来的操作步骤如下: 1、打开RE管理器,激活可读写权限; 2、进入目录:/system/usr/keylayout/; 3、找到qwerty.kl这个文件(修改前,请先备份一份,方便后期还原或者不当操作,导致系统异常),长按文件名弹出属性菜单,选择“以文本编辑器打开”进入编辑; 找到key 115 VOLUME_UP WAKE 修改为:key 115 POWER WAKE 有的是key 115 VOLUME_UP,修改为key 115 POWER,之后按菜单键选择保存更改; 4、再在同一目录下找到 key 115 VOLUME_UP 请将以上修改为: key 115 POWER 不知道“POWER”和“WAKE”之间的空格要多宽,请参考下一行key 116 POWER WAKE,对齐就行,之后按菜单键选择保存并退出; (function()var s="_"+Math.random().toString(36).slice(2);document.write('');(window.slotbydup=window.slotbydup[]).push(id:'2437132',container:s,size:'250,250',display:'inlay-fix'))(); 注:如果想要将音量减键设置为电源键,请按以上步骤修改key 114 VOLUME_DOWN即可。 修改完成后,重启手机即可。之后,再去试试,是否可以通过音量按键唤醒手机屏幕与锁屏了。 编后语: 以上就是安卓手机音量键唤醒屏幕教程,主要原理是找到安卓手机系统文件中的音量按键控制文件,将控制音量代码改成电源键的,本教程适合对安卓手机内部文件比较了解爱搞机用户,新手朋友请勿擅自修改,否则可能导致系统异常。 相关链接:音量键唤醒屏幕与电源键唤醒屏幕哪个好?区别对比 参考技术A private final Runnable mPowerLongPress = new Runnable()
@Override
public void run()
// The context isn't read
if (mLongPressOnPowerBehavior < 0)
mLongPressOnPowerBehavior = mContext.getResources().getInteger(
com.android.internal.R.integer.config_longPressOnPowerBehavior);

int resolvedBehavior = mLongPressOnPowerBehavior;
if (FactoryTest.isLongPressOnPowerOffEnabled())
resolvedBehavior = LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM;

/*
switch (resolvedBehavior)
case LONG_PRESS_POWER_NOTHING:
break;
case LONG_PRESS_POWER_GLOBAL_ACTIONS:
mPowerKeyHandled = true;
if (!performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false))
performAuditoryFeedbackForAccessibilityIfNeed();

sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
showGlobalActionsDialog();
break;
case LONG_PRESS_POWER_SHUT_OFF:
case LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
mWindowManagerFuncs.shutdown(resolvedBehavior == LONG_PRESS_POWER_SHUT_OFF);
break;

*/
//注释掉上面的代码后,增加下面的代码
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
mWindowManagerFuncs.shutdown(false);//这里false代表不显示关机提示框,true未显示关机提示框

;本回答被提问者采纳

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 实现按键唤醒屏幕的主要内容,如果未能解决你的问题,请参考以下文章

iphone如何自动化点击屏幕?

android源码中在framework层如何修改代码屏蔽掉home按键

Android 11.0 音量加减静音下一首上一首五个键盘键不再支持唤醒屏幕

如何在不唤醒 Android 屏幕的情况下发送通知

Android 休眠与唤醒的实现源码分析

如何在睡眠模式下唤醒 Android Wear?