覆盖Android设备中的锁定/电源按钮 [英] Overriding lock/power button in android devices

查看:95
本文介绍了覆盖Android设备中的锁定/电源按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道它是否有可能覆盖android中的电源按钮?

I would like to know if its possible to override power button in android?

我在Stack Overflow中阅读了很多帖子,有人说这是可能的,有人说这是不可能的(

I read a lot of posts in Stack Overflow, some saying it's possible and some saying it's not (example).

我想要实现的是-当用户使用我的应用程序时,他需要按5次锁定/电源按钮才能锁定屏幕.我不想只关闭锁定功能.

What I am trying to achieve is that - when user is using my app he needs to press the lock/power button 5 times to lock the screen. I dont want to override the swtich off functionality only the lock one.

反正还有获取电源按钮回调的信息吗?

Is there anyway to fetch power button callback?

我尝试了WakeLock并禁用了键盘锁,但是它并没有达到我的目的.

I tried WakeLock and disabled the keyguard but it does'nt serve my pupose.

我尝试了以下注释部分中发布的Shink链接的以下代码:

I tried out below code from Shink link posted in comments section below:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        // do what you want with the power button
        Toast.makeText(getApplicationContext(), "down", Toast.LENGTH_LONG).show();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        // do what you want with the power button
        Toast.makeText(getApplicationContext(), "up", Toast.LENGTH_LONG).show();
        return true;
    }
   return super.onKeyUp(keyCode, event);
}

仅当我按住电源按钮约2秒钟时,才会显示吐司味精.为什么呢?

The toast msg are only shown if I hold the power button for like 2 secs. why is that?

是否可以覆盖锁定屏幕的电源按钮功能?还是有其他方法可以实现我的目标?

Is it possible override power button functionality which locks the screen? Or is there any other way to achieve what I am trying to do?

推荐答案

出于安全目的,您不能禁用POWER_BUTTON.它以广播/订户模式运行...:(

You cannot disable POWER_BUTTON for security purpose. It runs with broadcast/subscriber patterns... :(

这篇关于覆盖Android设备中的锁定/电源按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆