OFF键preSS的Andr​​oid

OFF键preSS的Andr​​oid

本文介绍了检测ON / OFF键preSS的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检测电源按钮或锁屏按钮时pressed?当我的游戏暂停这种方式,它可能会导致游戏崩溃,因为我需要暂停线程,当它发生。

How can I detect the power button or lock screen button being pressed? When my game is paused in this way, it can cause the game to crash because I need to pause a thread when it happens.

推荐答案

从基督教的回答的:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        // do what you want with the power button
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

不过杰克巴西莱是正确的。除非你有一个很好的理由做一些特别的东西,当电源按钮为pressed,你应该使用标准的Andr​​oid生命周期的功能。

However Jake Basile is right. Unless you have a really good reason for doing something special when the Power Button is pressed, you should be using standard Android life-cycle functions.

当电源按钮为pressed它会调用你的应用程序的onPause()方法,而当你解锁设备,它会调用onResume()。这是你应该从崩溃的管理你的线程以prevent的应用程序。

When the Power Button is pressed it will call the onPause() method of your application, and when you unlock the device it will call onResume(). This is where you should be managing your thread to prevent the app from crashing.

在活动的的文档会给你的生命周期功能的详细描述,他们被称为,以及你应该如何使用它们。

The documentation on Activity's will give you a detailed description of life-cycle functions, when they are called, and how you should use them.

这篇关于检测ON / OFF键preSS的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!