Today, we'll focus on two functions.
(1) Double-click the back key to exit the program
(2) close to the perfect exit program
(1)
In many applications there is a function, that is, click the Back button, then prompt you then click the Back button to exit the program. have been very curious about how this is achieved, today is just the source of the security of the client source in the code to be stripped down, so here to study this very simple function.
public class Doubleclickexithelper {private Final Activity mactivity;private boolean isonkeybacking;private Handler Mhandler;private Toast mbacktoast;public doubleclickexithelper (activity activity) {mactivity = Activity;mhandler = new Handler (Looper.getmainlooper ());//Key point}/** * Activity OnKeyDown Event * */public boolean onKeyDown (int keycode, keyevent Event) {if (keycode! = Keyevent.keycode_back) {return false;} if (isonkeybacking) {mhandler.removecallbacks (onbacktimerunnable); if (mbacktoast! = null) {Mbacktoast.cancel ();} Exit Appmanager.getappmanager (). AppExit (mactivity); return true;} else {//isonkeybacking default value is False. So first click into here//java Boolean is the default value of isonkeybacking = true;if (Mbacktoast = = null) {Mbacktoast = Toast.maketext (mactivity, R.string.back_exit_tips, 2000);} Mbacktoast.show (); mhandler.postdelayed (onbacktimerunnable, +); return true;}} Private Runnable onbacktimerunnable = new Runnable () {@Overridepublic void run () {isonkeybacking = False;if (mbacktoast! = NULL) {//android with Toast.cancel () method to optimize the display of toast content//specific can Baidu Mbacktoast.cancel ();}};
Simple to understand is to listen to the return key. When you press it for the first time, use mbacktoast. Show (); displays a hint text. Here is "Press again to exit open source China". Duration 2000ms . And with this . mhandler. postdelayed an asynchronous operation is turned on, and the delay time is2000ms . Which means the Runnableonbacktimerunnable when the text disappears. It will call run . method. isonkeybacking value to false . Which means2sthen your second click does not work. Everything back to the original point.
and if you once in 2s Click again inside the word. AppManager. Getappmanager (). AppExit (mactivity);
The program is out.
when used
< Span style= "font-size:12pt" > mdoubleclickexithelper = New doubleclickexithelper ( this
To support this double exit activity , distribute the onkeydown event down:
/** * Listener return--whether to exit the program */public boolean onKeyDown (int keycode, KeyEvent event) {Boolean flag = true;if (keycode = = keyevent.ke Ycode_back) {//Whether exit applies return Mdoubleclickexithelper.onkeydown (KeyCode, event);} else if (keycode = = Keyevent.keycode_ MENU) {//Show Shortcut Bar & determine if login uihelper.showsettingloginorlogout (main.this,mgrid.getquickaction (0)); Mgrid.show ( Fbsetting, True);} else if (keycode = = Keyevent.keycode_search) {//Show search page Uihelper.showsearch (main.this);} else {flag = Super.onkeydown ( KeyCode, event);} return flag;}
This allows the entire function to be realized.
(2) close to the perfect exit program
Next, analyze the open source Chinese client This exit function, that is, the above
appmanager. GetAppManager (). AppExit ( mactivity implementation of this function
private static stack<activity> activitystack;/** * Exit application */public void AppExit (context context) {try {finishallact Ivity (); Activitymanager activitymgr= (Activitymanager) Context.getsystemservice (context.activity_service);// android2.2 should be followed by killbackgroundprocesses (String packagename) activitymgr.restartpackage (Context.getpackagename ()); System.exit (0);} catch (Exception e) {}}/** * ends all activity */public void finishallactivity () {for (int i = 0, size = activitystack.size (); I < size; i++) { if (null! = Activitystack.get (i)) { activitystack.get (i). Finish (); } } Activitystack.clear ();}
paste out the key source code everyone will understand it. There's a store app .AllActivity the stackactivitystack ;
Of course Add Activity The source code is not posted.
when the application exits, it's just a walk through the stack . . Then call Finish () . and finally callsystem.exit (0);gracefully, completely ending the entire program.
Oschina One of Android client source learning