对Android近期任务列表(RecentApplications)的简单分析.doc
,对Android近期任务列表(Recent Applications)的简单分析分类:Android开发2013-12-31 11:061599人阅读评论(0)收藏举报这里的近期任务列表就是长按Home键出来的那个Dialog,里面放着近期打开过的应用,当然3.0以上系统的多任务切换键也是。这个Dialog的实现在Android源码的/frameworks/base/policy/src/com/android/internal/policy/impl/RecentApplicationsDialog.java中。接下来就对这个源码分析一下。javaview plaincopy1. publicclassRecentApplicationsDialogextendsDialogimplementsOnClickListener2. /Elementsfordebuggingsupport3. /privatestaticfinalStringLOG_TAG="RecentApplicationsDialog"4. privatestaticfinalbooleanDBG_FORCE_EMPTY_LIST=false;5. 6. staticprivateStatusBarManagersStatusBar;7. 8. privatestaticfinalintNUM_BUTTONS=8;9. privatestaticfinalintMAX_RECENT_TASKS=NUM_BUTTONS*2;/allowforsomediscards10. 11. finalTextViewmIcons=newTextViewNUM_BUTTONS;12. ViewmNoAppsText;13. IntentFiltermBroadcastIntentFilter=newIntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);14. 15. classRecentTag16. ActivityManager.RecentTaskInfoinfo;17. Intentintent;18. 19. 20. HandlermHandler=newHandler();21. RunnablemCleanup=newRunnable()22. publicvoidrun()23. /dumpextramemorywerehangingonto24. for(TextViewicon:mIcons)25. icon.setCompoundDrawables(null,null,null,null);26. icon.setTag(null);27. 28. 29. ;30. 31. publicRecentApplicationsDialog(Contextcontext)32. super(context,com.android.internal.R.style.Theme_Dialog_RecentApplications);33. 34. 35. 36. /*37. *Wecreatetherecentapplicationsdialogjustonce,anditstaysaround(hidden)38. *untilactivatedbytheuser.39. *40. *seePhoneWindowManager#showRecentAppsDialog41. */42. Override43. protectedvoidonCreate(BundlesavedInstanceState)44. super.onCreate(savedInstanceState);45. 46. Contextcontext=getContext();47. 48. if(sStatusBar=null)49. sStatusBar=(StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);50. 51. 52. Windowwindow=getWindow();53. window.requestFeature(Window.FEATURE_NO_TITLE);54. window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);55. window.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,56. WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);57. window.setTitle("Recents");58. 59. setContentView(com.android.internal.R.layout.recent_apps_dialog);60. 61. finalWindowManager.LayoutParamsparams=window.getAttributes();62. params.width=WindowManager.LayoutParams.MATCH_PARENT;63. params.height=WindowManager.LayoutParams.MATCH_PARENT;64. window.setAttributes(params);65. window.setFlags(0,WindowManager.LayoutParams.FLAG_DIM_BEHIND);66. 67. /默认显示8个68. mIcons0=(TextView)findViewById(com.android.internal.R.id.button0);69. mIcons1=(TextView)findViewById(com.android.internal.R.id.button1);70. mIcons2=(TextView)findViewById(com.android.internal.R.id.button2);71. mIcons3=(TextView)findViewById(com.android.internal.R.id.button3);72. mIcons4=(TextView)findViewById(com.android.internal.R.id.button4);73. mIcons5=(TextView)findViewById(com.android.internal.R.id.button5);74. mIcons6=(TextView)findViewById(com.android.internal.R.id.button6);75. mIcons7=(TextView)findViewById(com.android.internal.R.id.button7);76. mNoAppsText=findViewById(com.android.internal.R.id.no_applications_message);77. 78. /关键在哪,你懂得.79. for(TextViewb:mIcons)80. b.setOnClickListener(this);81. 82. 83. 84. Override85. publicbooleanonKeyDown(intkeyCode,KeyEventevent)86. if(keyCode=KeyEvent.KEYCODE_TAB)87. /IgnoreallmetakeysotherthanSHIFT.Theappswitchkeycouldbea88. /fallbackactionchordedwithALT,METAorevenCTRLdependingonthekeymap.89. /DPadnavigationishandledbytheViewRootelsewhere.90. finalbooleanbackward=event.isShiftPressed();91. finalintnumIcons=mIcons.length;92. intnumButtons=0;93. while(numButtons<numIcons&&mIconsnumButtons.getVisibility()=View.VISIBLE)94. numButtons+=1;95. 96. if(numButtons!=0)97. intnextFocus=backward?numButtons-1:0;98. for(inti=0;i<numButtons;i+)99. if(mIconsi.hasFocus()100. if(backward)101. nextFocus=(i+numButtons-1)%numButtons;102. else103. nextFocus=(i+1)%numButtons;104. 105. break;106. 107. 108. finalintdirection=backward?View.FOCUS_BACKWARD:View.FOCUS_FORWARD;109. if(mIconsnextFocus.requestFocus(direction)110. mIconsnextFocus.playSoundEffect(111. SoundEffectConstants.getContantForFocusDirection(direction);112. 113. 114. 115. /ThedialogalwayshandlesthekeytopreventtheViewRootfrom116. /performingthedefaultnavigationitself.117. returntrue;118. 119. 120. returnsuper.onKeyDown(keyCode,event);121. 122. 123. /*124. *Dismissthedialogandswitchtotheselectedapplication.125. */126. publicvoiddismissAndSwitch()127. finalintnumIcons=mIcons.length;128. RecentTagtag=null;129. for(inti=0;i<numIcons;i+)130. if(mIconsi.getVisibility()!=View.VISIBLE)131. break;132. 133. if(i=0|mIconsi.hasFocus()134. tag=(RecentTag)mIconsi.getTag();135. if(mIconsi.hasFocus()136. break;137. 138. 139. 140. if(tag!=null)141. switchTo(tag);142. 143. dismiss();144. 145. 146. /*147. *Handlerforuserclicks.Ifabuttonwasclicked,launchthecorrespondingactivity.148. */149. publicvoidonClick(Viewv)150. for(TextViewb:mIcons)151. if(b=v)152. RecentTagtag=(RecentTag)b.getTag();153. switchTo(tag);154. break;155. 156. 157. dismiss();158. 159. 160. /161. privatevoidswitchTo(RecentTagtag)162. if(tag.info.id>=0)163. /Thisisanactivetask;itshouldjustgototheforeground.164. finalActivityManageram=(ActivityManager)165. getContext().getSystemService(Context.ACTIVITY_SERVICE);166. am.moveTaskToFront(tag.info.id,ActivityManager.MOVE_TASK_WITH_HOME);167. elseif(tag.intent!=null)168. tag.intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY169. |Intent.FLAG_ACTIVITY_TASK_ON_HOME);170. try171. getContext().startActivity(tag.intent);172. catch(ActivityNotFoundExceptione)173. Log.w("Recent","Unabletolaunchrecenttask",e);174. 175. 176. 177. 178. /*179. *Setupandshowtherecentactivitiesdialog.180. */181. Override182. publicvoidonStart()183. super.onStart();184. reloadButtons();185. if(sStatusBar!=null)186. sStatusBar.disable(StatusBarManager.DISABLE_EXPAND);187. 188. 189. /receivebroadcasts190. getContext().registerReceiver(mBroadcastReceiver,mBroadcastIntentFilter);191. 192. mHandler.removeCallbacks(mCleanup);193. 194. 195. /*196. *Dismisstherecentactivitiesdialog.197. */198. Override199. publicvoidonStop()200. super.onStop();201. 202. if(sStatusBar!=null)203. sStatusBar.disable(StatusBarManager.DISABLE_NONE);204. 205. 206. /stopreceivingbroadcasts207. getContext().unregisterReceiver(mBroadcastReceiver);208. 209. mHandler.postDelayed(mCleanup,100);210. 211. 212. /*213. *Reloadthe6buttonswithrecentactivities214. */215. privatevoidreloadButtons()216. 217. finalContextcontext=getContext();218. finalPackageManagerpm=context.getPackageManager();219. finalActivityManageram=(ActivityManager)220. context.getSystemService(Context.ACTIVITY_SERVICE);221. finalList<ActivityManager.RecentTaskInfo>recentTasks=222. am.getRecentTasks(MAX_RECENT_TASKS,ActivityManager.RECENT_IGNORE_UNAVAILABLE);223. 224. ActivityInfohomeInfo=225. newIntent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)226. .resolveActivityInfo(pm,0);227. 228. IconUtilitiesiconUtilities=newIconUtilities(getContext();229. 230. /Performancenote:OurandroidperformanceguidesaystopreferIteratorwhen231. /usingaListclass,butbecauseweknowthatgetRecentTasks()alwaysreturns232. /anArrayList<>,welluseasimpleindexinstead.233. intindex=0;234. intnumTasks=recentTasks.size();235. for(inti=0;i<numTasks&&(index<NUM_BUTTONS);+i)236. finalActivityManager.RecentTaskInfoinfo=recentTasks.get(i);237. 238. /fordebugpurposesonly,disallowfirstresulttocreateemptylists239. if(DBG_FORCE_EMPTY_LIST&&(i=0)continue;240. 241. Intentintent=newIntent(info.baseIntent);242. if(info.origActivity!=null)243. intent.setComponent(info.origActivity);244. 245. 246. /Skipthecurrenthomeactivity.247. if(homeInfo!=null)248. if(homeInfo.packageName.equals(249. intent.getComponent().getPackageName()250. &&homeInfo.name.equals(251. intent.getComponent().getClassName()252. continue;253. 254. 255. 256. intent.setFlags(intent.getFlags()&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)257. |Intent.FLAG_ACTIVITY_NEW_TASK);258. finalResolveInforesolveInfo=pm.resolveActivity(intent,0);259. if(resolveInfo!=null)260. finalActivityInfoactivityInfo=resolveInfo.activityInfo;261. finalStringtitle=activityInfo.loadLabel(pm).toString();262. Drawableicon=activityInfo.loadIcon(pm);263. 264. if(title!=null&&title.length()>0&&icon!=null)265. finalTextViewtv=mIconsindex;266. tv.setText(title);267. icon=iconUtilities.createIconDrawable(icon);268. tv.setCompoundDrawables(null,icon,null,null);269. RecentTagtag=newRecentTag();270. tag.info=info;271. tag.intent=intent;272. tv.setTag(tag);273. tv.setVisibility(View.VISIBLE);274. tv.setPressed(false);275. tv.clearFocus();276. +index;277. 278. 279. 280. 281. /handlethecaseof"noiconstoshow"282. mNoAppsText.setVisibility(index=0)?View.VISIBLE:View.GONE);283. 284. /hidetherest285. for(;index<NUM_BUTTONS;+index)286. mIconsindex.setVisibility(View.GONE);287. 288. 289. 290. /*291. *ThisisthelistenerfortheACTION_CLOSE_SYSTEM_DIALOGSintent.Itsanindicationthat292. *weshouldcloseourselvesimmediately,inordertoallowahigher-priorityUItotakeover293. *(e.g.phonecallreceived).294. */295. privateBroadcastReceivermBroadcastReceiver=newBroadcastReceiver()296. Override297. publicvoidonReceive(Contextcontext,Intentintent)298. Stringaction=intent.getAction();299. if(Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)300. Stringreason=intent.getStringExtra(PhoneWindowManager.SYSTEM_DIALOG_REASON_KEY);301. if(!PhoneWindowManager.SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)302. dismiss();303. 304. 305. 306. ;307. 308. 309. RecentApplicationsDialog.java完整源码从源码可以看出,关键部分有三处。一个很关键的内部类:/ 每个任务都包含一个Tag,这个Tag保存着这个App的一些非常有用的信息 class RecentTag ActivityManager.RecentTaskInfo info; Intent intent; 这个RecentTag保存在每个近期任务的图标里,并且保存着这个任务的原始信息。刚启动Dialog时对每个任务的初始化:javaview plaincopy1. privatevoidreloadButtons()2. 3. finalContextcontext=getContext();4. finalPackageManagerpm=context.getPackageManager();5. finalActivityManageram=(ActivityManager)6. context.getSystemService(Context.AC