Android can click the back button twice to exit the application

Idea: capturing user keys in Android is in the onkeydown method. You only need to judge whether the user keys are keycode_ Back is the back key, and the rest is to judge the time interval between two clicks of the back key.

The first implementation method:

In the exit method, the value of isexit will be judged first. If it is false, it will be set to true. At the same time, a prompt will pop up, and a message will be sent out after 2000 milliseconds (2 seconds) to restore this value to false in the handler. If you press the back key again within 2 seconds of the message sending interval, the exit method will be executed again. At this time, if the value of isexit is true, the exit method will be executed.

The second implementation method:

Judge whether the time difference between the user's two keys is within an expected value. If yes, exit directly. If not, prompt the user to press the back key again to exit.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>