覆盖电源按钮就像Home键 [英] Override Power button just like Home button

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

问题描述

嗯,我做的,我想禁用设备的所有硬按键的东西。

像电源,主页,音量增大,音量减小,搜索硬按键,返回。

我已经成功覆盖几乎所有的按钮<一href="http://stackoverflow.com/questions/10025660/override-home-and-back-button-is-case-a-boolean-is-true/10025904#10025904">here除了动力。

所以,我只是想让你的人看到,并请分享一些想法,这样我就可以得到侥幸成功。

我收到的长preSS 在电源的KeyEvent onDispatchKeyEvent(),以同样的方式我要赶短按一下的相同。此外,当pressing功率我也试图阻止的屏幕关闭的通过得到的广播 SCREEN_OFF ,我成功地接受它,但我无法处理它。

感谢。

然后,我创造了一个ReceiverScreen其接收的广播屏幕的开/关

ReceiverScreen.java

 公共类ReceiverScreen扩展的BroadcastReceiver {

    公共静态布尔wasScreenOn = TRUE;

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        如果(intent.getAction()。等于(Intent.ACTION_SCREEN_OFF)){
            //做任何你需要做的
            wasScreenOn = FALSE;
        }否则,如果(intent.getAction()。等于(Intent.ACTION_SCREEN_ON)){
            //,做任何你需要做的
            wasScreenOn = TRUE;
        }
    }
}
 

DisableHardButton.java

 公共类DisableHardButton延伸活动{

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.main);
    IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver的mReceiver =新ReceiverScreen();
    registerReceiver(mReceiver,过滤器);
    }

@覆盖
    保护无效的onPause(){
        //当屏幕即将关闭
        如果(ScreenReceiver.wasScreenOn){
            //这是当的onPause()被调用的系统的情况下,由于屏幕状态变化
            的System.out.println(屏幕已关闭);

    } 其他 {
        //这是当的onPause()被调用时,屏幕的状态并没有改变
    }
    super.onPause();
}

@覆盖
保护无效onResume(){
    //只有当屏幕开启
    如果(!ScreenReceiver.wasScreenOn){
        //这是当onResume()被调用由于屏幕状态变化
        的System.out.println(显示屏被翻开);
    } 其他 {
        //这是当onResume()被调用时,屏幕的状态并没有改变
    }
    super.onResume();
}
}
 

解决方案

唷。这是一个相当争辩的问题,具有很大背后解说。

首先,我要改写你的问题有点。如果我没有理解清楚,你想禁用设备上的所有物理按键为您的活动的持续时间。这是包容性的电源按钮,您可以通过处理 ACTION_SCREEN_OFF 意图检测。您当前的(成功)解决此方案是广播的 ACTION_SCREEN_ON ,踢屏幕缓过劲来的时候它关闭,提供的主机正确实现这一点。

您想现在要多走一英里,通过渲染这一行动是不必要的,如果(且仅当)你能捕获并处理 ACTION_SCREEN_OFF 前,先要发送到系统。这是可能的,如果是这样,怎么样?

这都有一些讨论。简短的版本,但是,很简单:这是不可能的,没有自定义修改固件或Android操作系统的核心,对于设备数量有限

Android系统,就被记录在案,这定义为<一个href="http://developer.android.com/reference/android/content/Context.html#sendBroadcast%28android.content.Intent%29">broadcast行动的。继发布 - 订阅模式的消息传播,这条消息会通知这个动作的有关各方。因为该消息由系统发送,因为消息堆栈是由系统管理,并且因为该消息是也接收的由系统,你的code根本没有在右边注入地方阻止此消息的接收。

此外,对于某些设备,这将是一个的物理的开关,没有任何编程中断任何责任。充其量,Android系统能希望来处理,当它发生的事件,这是precisely为什么这个广播消息是一些在服务栈的一个特例。要尽我的知识和基于稀疏文件在这种情况下,这就是为什么它不是一个彻头彻尾的开箱即用,支持的功能,我们考虑任何可能的载体之前被滥用。

您最好的追索权实际上是一个非常简单的一个,如果你需要修改物理硬件有问题的设备的能力:限制访问,修改,增加或以其他方式禁用物理电源按钮。在许多情况下,这是不可取的,但这部作品在你的Andr​​oid设备被使用,例如情况下,作为点的出售或公共服务的机器。如果这是行不通的,将物理限制接入电源按钮可能是所有的需要,处理的情况下(例如,现货尝试切换屏幕),通过发送 ACTION_SCREEN_ON

记住,这种策略并非万无一失。做到这一点胜任和文件的方法好,免得你成为饲料下一 2600揭露

祝您好运与您的应用程序。

Well, I am doing something in which I want to disable all hard buttons of the device.

Hard buttons like Power, Home, Volume up, Volume down, Search, Back.

I have successfully overridden almost all buttons here except Power.

So I just want you people to see and please share some ideas so that I get can away with it.

I am getting the long press Power keyevent in onDispatchKeyEvent(), in the same way I want to catch the short click of the same. Moreover when pressing power I also tried to stop Screen off by getting the Broadcast of SCREEN_OFF and I succeeded in receiving it but I was not able to handle it.

Thanks.

Then, I had created a ReceiverScreen which receives broadcast of Screen on/off

ReceiverScreen.java

public class ReceiverScreen extends BroadcastReceiver {

    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            // do whatever you need to do here
            wasScreenOn = false;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // and do whatever you need to do here
            wasScreenOn = true;
        }
    }
}

DisableHardButton.java

public class DisableHardButton extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ReceiverScreen();
    registerReceiver(mReceiver, filter);
    }

@Override
    protected void onPause() {
        // when the screen is about to turn off
        if (ScreenReceiver.wasScreenOn) {
            // this is the case when onPause() is called by the system due to a screen state change
            System.out.println("SCREEN TURNED OFF");

    } else {
        // this is when onPause() is called when the screen state has not changed
    }
    super.onPause();
}

@Override
protected void onResume() {
    // only when screen turns on
    if (!ScreenReceiver.wasScreenOn) {
        // this is when onResume() is called due to a screen state change
        System.out.println("SCREEN TURNED ON");
    } else {
        // this is when onResume() is called when the screen state has not changed
    }
    super.onResume();
}
}

解决方案

Phew. This is quite a contended question, with a great deal of commentary behind it.

Let me begin by rephrasing your question a bit. If I understand clearly, you'd like to disable all physical buttons on the device for the duration of your activity. This is inclusive of the power button, which you detect by handling the ACTION_SCREEN_OFF intent. Your current (successful) workaround for this scenario is to broadcast an ACTION_SCREEN_ON, kicking the screen back to life when it's turned off, provided the host implements this correctly.

You'd now like to go the extra mile, by rendering this action unnecessary, if (and only if) you are able to catch and handle ACTION_SCREEN_OFF before it gets sent to the system. Is this possible, and if so, how?

This bears some discussion. The short version, however, is simple: this is not possible, without custom modification to your firmware or the core of the Android operating system, for a limited number of devices.

The Android system, as far as is documented, defines this as a broadcast action. Following the publish-subscribe pattern of message propagation, this message will notify all concerned parties of this action. Because this message is sent by the system, because the message stack is managed by the system, and because the message is also received by the system, your code simply isn't injected in the right place to block the reception of this message.

Furthermore, for some devices, this will be a physical switch that has no programmatic interrupt whatsoever. At best, the Android system can hope to handle for the event when it happens, which is precisely why this broadcast message is something of a special case in the service stack. To the best of my knowledge and based upon the sparse documentation on this scenario, this is why it's not an out-of-the-box, supported feature, before we consider any of the possible vectors for abuse.

Your best recourse is actually a much simpler one, if you have the ability to modify the physical hardware for the device in question: restrict access to, mangle, or otherwise disable the physical power button. In many scenarios this is undesirable, but this works in situations where your Android device is being used, for example, as a point-of-sale or a public service machine. If this is unworkable, placing physical restrictions on access to the power button may be all that's required, handling for the remainder of cases (for example, spot attempts to toggle the screen) by sending ACTION_SCREEN_ON.

Just remember, this strategy isn't foolproof. Do this competently and document the procedure well, lest you become fodder for the next 2600 exposé.

Best of luck with your application.

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

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