The recent Task box (recently opened applications) is actually a system-level dialog box, that is, a view popped up by pressing the Home Key of the mobile phone.
Path in source code: D: \ tools \ android4.0.1 \ frameworks \ base \ Policy \ SRC \ com \ Android \ internal \ Policy \ impl \ recentapplicationsdialog. Java
1. display mode. This dialog box is displayed in the showrecentappsdialog method in the phonewindowmanager (same path as recentapplicationsdialog. Java) class.
/** * Create (if necessary) and launch the recent apps dialog, or hide it if it is * already shown. */ void showOrHideRecentAppsDialog(final int heldModifiers, final boolean dismissIfShown) { mHandler.post(new Runnable() { @Override public void run() { if (mRecentAppsDialog == null) { mRecentAppsDialog = new RecentApplicationsDialog(mContext); } if (mRecentAppsDialog.isShowing()) { if (dismissIfShown) { mRecentAppsDialog.dismiss(); } } else { mRecentAppsDialog.setHeldModifiers(heldModifiers); mRecentAppsDialog.show(); } } }); }
2. When creating a dialog box, configure some parameters, such as filling the parent form with no title. More importantly, set it to a system-level dialog box.
/*** The create recent application dialog box is only once. It will be left (hidden) until the user is activated. ** @ See is displayed in this class (phonewindowmanager # showrecent1_dialog) */@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); Context context = getcontext (); If (sstatusbar = NULL) {sstatusbar = (statusbarmanager) context. getsystemservice (context. status_bar_service);} // obtain the form. Note: The window is the form of the entire mobile phone screen, not just the window of the dialog box = getwindow (); // set the window without a title. requestfeature (window. feature_no_title); // set this dialog box to the system-level dialog box window. settype (windowmanager. layoutparams. type_system_dialog); window. setflags (windowmanager. layoutparams. flag_alt_focusable_im, windowmanager. layoutparams. flag_alt_focusable_im); window. settitle ("recents"); setcontentview (COM. android. internal. r. layout. recent_apps_dialog); // set the parameters in the dialog box: Fill the final windowmanager of the parent form with the width and height. layoutparams Params = Window. getattributes (); Params. width = windowmanager. layoutparams. match_parent; Params. height = windowmanager. layoutparams. match_parent; window. setattributes (Params); window. setflags (0, windowmanager. layoutparams. flag_dim_behind); micons [0] = (textview) findviewbyid (COM. android. internal. r. id. button0); micons [1] = (textview) findviewbyid (COM. android. internal. r. id. button1); micons [2] = (textview) findviewbyid (COM. android. internal. r. id. button2); micons [3] = (textview) findviewbyid (COM. android. internal. r. id. button3); micons [4] = (textview) findviewbyid (COM. android. internal. r. id. button4); micons [5] = (textview) findviewbyid (COM. android. internal. r. id. button5); micons [6] = (textview) findviewbyid (COM. android. internal. r. id. button6); micons [7] = (textview) findviewbyid (COM. android. internal. r. id. button7); mnoappstext = findviewbyid (COM. android. internal. r. id. no_applications_message); // set the click event for each textview (textview B: micons) {B. setonclicklistener (this );}}
3. Load the most recent activity for the eight buttons. If there are more than eight recently played Application Task stacks, you can only get eight
/*** Load the most recent activity for the eight buttons. If there are more than eight recently typed Application Task stacks, you can only get eight, */private void reloadbuttons () {final context = getcontext (); Final packagemanager PM = context. getpackagemanager (); Final activitymanager AM = (activitymanager) context. getsystemservice (context. activity_service); // obtain the recenttaskinfofinal list of recently opened applications <activitymanager. recenttaskinfo> recenttasks = aM. getrecenttasks (max_recent_tasks, activitymanager. Recent_ignore_unavailable); activityinfo homeinfo = new intent (intent. action_main ). addcategory (intent. category_home ). resolveactivityinfo (PM, 0); iconutilities = new iconutilities (getcontext (); // performance note: the android performance Guide recommends the iterator to traverse the set because it knows getrecenttasks () always Returns An arraylist // <>. We will use a simple exponential opposite. Int Index = 0; int numtasks = recenttasks. size (); For (INT I = 0; I <numtasks & (index <num_buttons); ++ I) {final activitymanager. recenttaskinfo info = recenttasks. get (I); // The object is used for debugging, but the first result is not allowed to create an empty list if (dbg_force_empty_list & (I = 0) continue; intent intent = new intent (info. baseintent); If (info. origactivity! = NULL) {intent. setcomponent (info. origactivity);} // skip the current launcher data, that is, the current launcher is not used as a recently opened task. If (homeinfo! = NULL) {If (homeinfo. packagename. equals (intent. getcomponent (). getpackagename () & homeinfo. name. equals (intent. getcomponent (). getclassname () {continue ;}// the following Code encapsulates some data for each corresponding textviewintent. setflags (intent. getflags ()&~ Intent. flag_activity_reset_task_if_needed) | intent. flag_activity_new_task); Final resolveinfo = PM. resolveactivity (intent, 0); If (resolveinfo! = NULL) {final activityinfo = resolveinfo. activityinfo; Final String title = activityinfo. loadlabel (PM ). tostring (); drawable icon = activityinfo. loadicon (PM); If (title! = NULL & title. Length ()> 0 & icon! = NULL) {final textview TV = micons [Index]; TV. settext (title); icon = iconutilities. createicondrawable (icon); TV. setcompounddrawables (null, icon, null, null); recenttag tag = new recenttag (); tag.info = Info; tag. intent = intent; // The settag is a magic thing integrated in the view. I will express my opinions later. TV. settag (TAG); TV. setvisibility (view. visible); TV. setpressed (false); TV. clearfocus (); ++ index ;}}// process mnoappstext without icons. setvisibility (INDE X = 0 )? View. visible: View. gone); // hide other icons for (; index <num_buttons; ++ index) {micons [Index]. setvisibility (view. gone );}}
4. Set and display the latest activity dialog box.
/*** Set and display the latest activity dialog box. * // @ Overridepublic void onstart () {super. onstart (); reloadbuttons (); // disable the status bar if (sstatusbar! = NULL) {sstatusbar. Disable (statusbarmanager. disable_expand);} // register and receive broadcast getcontext (). registerreceiver (receiver, mbroadcastintentfilter); mhandler. removecallbacks (mcleanup );}
5. process the finger-pressed action.
/*** Process the program when the finger is pressed */@ overridepublic Boolean onkeydown (INT keycode, keyevent event)
6. process the finger movement
/*** Process the program when the finger is lifted */@ overridepublic Boolean onkeyup (INT keycode, keyevent event)
7. Process User-clicked events
/*** Click the handler. If a button is clicked, the corresponding activity is started. */@ Overridepublic void onclick (view v) {for (textview B: micons) {If (B = V) {recenttag tag = (recenttag) B. gettag (); switchto (TAG); break ;}} dismiss ();}
8. Switch to the clicked application.
/*** Switch to the clicked application ** @ Param tag */private void switchto (recenttag tag) {If (tag.info. id> = 0) {// This is an active task, so move it to the front of the latest Task final activitymanager AM = (activitymanager) getcontext (). getsystemservice (context. activity_service); AM. movetasktofront (tag.info. ID, activitymanager. move_task_with_home);} else if (tag. intent! = NULL) {tag. intent. addflags (intent. flag_activity_launched_from_history | intent. flag_activity_task_on_home); try {getcontext (). startactivity (tag. intent);} catch (activitynotfoundexception e) {log. W ("Recent", "unable to launch recent Task", e );}}}
9. process the broadcast in the receive close dialog box.
/*** This is the broadcast intended for listening to the disable system dialog box. Close yourself immediately after receiving the broadcast, in order to allow the high-priority UI to take over (such as the phone received ). */Private broadcastreceiver mbroadcastreceiver = new broadcastreceiver () {@ overridepublic void onreceive (context, intent) {string action = intent. getaction (); If (intent. action_close_system_dialogs.equals (Action) {string reason = intent. getstringextra (phonewindowmanager. system_dialog_reason_key); If (! Phonewindowmanager. system_dialog_reason_recent_assist.equals (reason) {dismiss ();}}}};
If you need to close this dialog box from another application, send a broadcast to the system:
Intent i = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);this.sendBroadcast(i);