Because my project has programs that need to be run in the background. So I want to make something similar to QQ mobile phones. Hide the main program and click the status bar icon to restore it to the previous page.
The following unit is the listener return button, used to hide the main program
@ Override public Boolean onkeydown (INT keycode, keyevent event) {If (keycode = keyevent. keycode_back) {alertdialog. onclicklistener lsn = new alertdialog. onclicklistener () {@ override public void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub if (which = dialoginterface. button_negative) {return;} If (false) {// dealcopyingonexit ();} else {shownotification (R. drawable. logo2, "text next to the image", "XXXX", "content"); intent I = new intent (intent. action_main); I. setflags (intent. flag_activity_new_task); I. addcategory (intent. category_home); startactivity (I) ;}}; new alertdialog. builder (this ). setmessage (getstring (R. string. sure_exit )). setpositivebutton ("\ t hide \ t", LSN ). setnegativebutton ("\ t cancel \ t", LSN ). create (). show (); Return true;} return Super. onkeydown (keycode, event );}
Create a notification
Public void shownotification (INT icon, string tickertext, String title, string content) {// notification manager Nm = (icationicationmanager) getsystemservice (icationication_service); Notification = new notification (icon, tickertext, system. currenttimemillis (); // The following parameters are the small icon displayed in the top notification bar, and the text next to the small icon (temporarily displayed, automatically disappears) the current time of the system (do not understand what this is useful) // notification. defaults = notification. default_vibrate; // This indicates whether the sound or vibration is played at the same time, and the sound is notification. default_sound // the vibration is notification. default_vibrate; // light is notification. default_lights, it seems that there is no response on my milestone // All are notification. default_all // if it is vibration or all, it must be in androidmanifest. XML join vibration permission pendingintent Pt = pendingintent. getactivity (this, 0, new intent (this, vncesmain. class), 0); // The action after the notification is clicked. Here, the acticity notification of main is returned. setlatesteventinfo (this, title, content, pt); nm. Y (ication_id _id, notification );}
The most important thing is. If you do not add the following sentence, when you click the small picture in the status bar, it will re-create a program.
Add
Android: launchmode = "singletask"
Singletask: As the name implies, only one activity is retained in the stack.
If singletask is set for a desktop activity, all other activities are cleared from the stack when the application is restarted next time, leaving only the activity on the main interface. All the activities on the main interface are opened after the application is restarted.