In the source code, the fire ant completely encapsulates the function of the double-exit program: Doubleclickexithelper class
This type of source analysis is as follows:
1 in the constructor, the device context is passed in, the interface of exiting function is implemented, and the initialization of the message processor is completed. If you want a cue bar, you also need a toast object.
2 since it is fully encapsulated, then we need to pass the key event and key code in, because the interface rewrite onkeydown event needs to return a Boolean value, then in the Doubleclickexithelper class also customizes a return boolean value of the The OnKeyDown function.
3onKeyDown function:
3.1 If the key is not pressed, return false;isonkeybacking as a global variable, initialize doubleclickexithelper object, the default value is False, When the first click is False, the isonkeybacking is set to true to finish the toast initialization, Execute a business with Handler.post (runnable) delay-If the toast is still displayed, it is manually shut down directly. When the second click isonkeybacking is already true, this time there is a possibility: runnable delay execution of the business has not been executed (from the source we can see, this time is 2 seconds, that is, 2 seconds to click the return button again), we can first remove the delay to perform the task, Then directly close the toast, perform the operation of the close interface, and so on. Returns true after execution, representing a response to the keydown event.
3.2 The advantages of this method in addition to encapsulation, there is a manual closure of the toast, generally in use, the display time is not short is a long, but after the interface is closed, when the toast display after the shutdown when the device context is not found, there is a program exited, but the toast has always shown that I have encountered this problem.
Read the code, is a good habit, you insist!
Open source China oschina Android Client Source code Analysis (10) Double Exit program