對Android近期任務(wù)列表(RecentApplications)的簡單_第1頁
對Android近期任務(wù)列表(RecentApplications)的簡單_第2頁
對Android近期任務(wù)列表(RecentApplications)的簡單_第3頁
對Android近期任務(wù)列表(RecentApplications)的簡單_第4頁
對Android近期任務(wù)列表(RecentApplications)的簡單_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、對Android近期任務(wù)列表(Recent Applications)的簡單分析分類:Android開發(fā)2013-12-31 11:061599人閱讀評論(0)收藏舉報這里的近期任務(wù)列表就是長按Home鍵出來的那個Dialog,里面放著近期打開過的應(yīng)用,當(dāng)然3.0以上系統(tǒng)的多任務(wù)切換鍵也是。這個Dialog的實現(xiàn)在Android源碼的/frameworks/base/policy/src/com/android/internal/policy/impl/RecentApplicationsDialog.java中。接下來就對這個源碼分析一下。javaview plaincopy1. publi

2、cclassRecentApplicationsDialogextendsDialogimplementsOnClickListener2. /Elementsfordebuggingsupport3. /privatestaticfinalStringLOG_TAG=RecentApplicationsDialog;4. privatestaticfinalbooleanDBG_FORCE_EMPTY_LIST=false;5. 6. staticprivateStatusBarManagersStatusBar;7. 8. privatestaticfinalintNUM_BUTTONS=

3、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

4、;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(

5、Contextcontext)32. super(context,ernal.R.style.Theme_Dialog_RecentApplications);33. 34. 35. 36. /*37. *Wecreatetherecentapplicationsdialogjustonce,anditstaysaround(hidden)38. *untilactivatedbytheuser.39. *40. *seePhoneWindowManager#showRecentAppsDialog41. */42. Override43. protectedvo

6、idonCreate(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.FEATUR

7、E_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(ernal.R.layout.recent_apps_dialo

8、g);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. /默認(rèn)顯示8

9、個68. mIcons0=(TextView)findViewById(ernal.R.id.button0);69. mIcons1=(TextView)findViewById(ernal.R.id.button1);70. mIcons2=(TextView)findViewById(ernal.R.id.button2);71. mIcons3=(TextView)findViewById(ernal.R.id.button3);72. mIcons4=(TextVi

10、ew)findViewById(ernal.R.id.button4);73. mIcons5=(TextView)findViewById(ernal.R.id.button5);74. mIcons6=(TextView)findViewById(ernal.R.id.button6);75. mIcons7=(TextView)findViewById(ernal.R.id.button7);76. mNoAppsText=findViewById(com.androi

11、ernal.R.id.no_applications_message);77. 78. /關(guān)鍵在哪,你懂得.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. /fallback

12、actionchordedwithALT,METAorevenCTRLdependingonthekeymap.89. /DPadnavigationishandledbytheViewRootelsewhere.90. finalbooleanbackward=event.isShiftPressed();91. finalintnumIcons=mIcons.length;92. intnumButtons=0;93. while(numButtonsnumIcons&mIconsnumButtons.getVisibility()=View.VISIBLE)94. numButtons+

13、=1;95. 96. if(numButtons!=0)97. intnextFocus=backward?numButtons-1:0;98. for(inti=0;inumButtons;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_BACKWA

14、RD:View.FOCUS_FORWARD;109. if(mIconsnextFocus.requestFocus(direction)110. mIconsnextFocus.playSoundEffect(111. SoundEffectConstants.getContantForFocusDirection(direction);112. 113. 114. 115. /ThedialogalwayshandlesthekeytopreventtheViewRootfrom116. /performingthedefaultnavigationitself.117. returntr

15、ue;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=0)163. /Thisisanactivetask;itshouldjustgototheforegroun

16、d.164. finalActivityManageram=(ActivityManager)165. getContext().getSystemService(Context.ACTIVITY_SERVICE);166. am.moveTaskToFront(.id,ActivityManager.MOVE_TASK_WITH_HOME);167. elseif(ent!=null)168. ent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY169. |Intent.FLAG_ACTIV

17、ITY_TASK_ON_HOME);170. try171. getContext().startActivity(ent);172. catch(ActivityNotFoundExceptione)173. Log.w(Recent,Unabletolaunchrecenttask,e);174. 175. 176. 177. 178. /*179. *Setupandshowtherecentactivitiesdialog.180. */181. Override182. publicvoidonStart()183. super.onStart();184. reloa

18、dButtons();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. *Dismisstherecentactivitiesdi

19、alog.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. 2

20、11. 212. /*213. *Reloadthe6buttonswithrecentactivities214. */215. privatevoidreloadButtons()216. 217. finalContextcontext=getContext();218. finalPackageManagerpm=context.getPackageManager();219. finalActivityManageram=(ActivityManager)220. context.getSystemService(Context.ACTIVITY_SERVICE);221. fina

21、lListrecentTasks=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();22

22、9. 230. /Performancenote:OurandroidperformanceguidesaystopreferIteratorwhen231. /usingaListclass,butbecauseweknowthatgetRecentTasks()alwaysreturns232. /anArrayList,welluseasimpleindexinstead.233. intindex=0;234. intnumTasks=recentTasks.size();235. for(inti=0;inumTasks&(index0&icon!=null)265. finalTe

23、xtViewtv=mIconsindex;266. tv.setText(title);267. icon=iconUtilities.createIconDrawable(icon);268. tv.setCompoundDrawables(null,icon,null,null);269. RecentTagtag=newRecentTag();270. =info;271. ent=intent;272. tv.setTag(tag);273. tv.setVisibility(View.VISIBLE);274. tv.setPressed(false);

24、275. tv.clearFocus();276. +index;277. 278. 279. 280. 281. /handlethecaseofnoiconstoshow282. mNoAppsText.setVisibility(index=0)?View.VISIBLE:View.GONE);283. 284. /hidetherest285. for(;indexNUM_BUTTONS;+index)286. mIconsindex.setVisibility(View.GONE);287. 288. 289. 290. /*291. *Thisisthelistenerforthe

25、ACTION_CLOSE_SYSTEM_DIALOGSintent.Itsanindicationthat292. *weshouldcloseourselvesimmediately,inordertoallowahigher-priorityUItotakeover293. *(e.g.phonecallreceived).294. */295. privateBroadcastReceivermBroadcastReceiver=newBroadcastReceiver()296. Override297. publicvoidonReceive(Contextcontext,Inten

26、tintent)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. ;30

27、7. 308. 309. RecentApplicationsDialog.java完整源碼從源碼可以看出,關(guān)鍵部分有三處。一個很關(guān)鍵的內(nèi)部類:/ 每個任務(wù)都包含一個Tag,這個Tag保存著這個App的一些非常有用的信息 class RecentTag ActivityManager.RecentTaskInfo info; Intent intent; 這個RecentTag保存在每個近期任務(wù)的圖標(biāo)里,并且保存著這個任務(wù)的原始信息。剛啟動Dialog時對每個任務(wù)的初始化:javaview plaincopy1. privatevoidreloadButtons()2. 3. finalCon

28、textcontext=getContext();4. finalPackageManagerpm=context.getPackageManager();5. finalActivityManageram=(ActivityManager)6. context.getSystemService(Context.ACTIVITY_SERVICE);7. 8. /拿到最近使用的應(yīng)用的信息列表9. finalListrecentTasks=10. am.getRecentTasks(MAX_RECENT_TASKS,ActivityManager.RECENT_IGNORE_UNAVAILABLE

29、);11. 12. /自制一個homeactivityinfo,用來區(qū)分13. ActivityInfohomeInfo=14. newIntent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)15. .resolveActivityInfo(pm,0);16. 17. IconUtilitiesiconUtilities=newIconUtilities(getContext();18. 19. intindex=0;20. intnumTasks=recentTasks.size();21. /開始初始化每個任務(wù)的信息22. f

30、or(inti=0;inumTasks&(index0&icon!=null)50. finalTextViewtv=mIconsindex;51. tv.setText(title);52. icon=iconUtilities.createIconDrawable(icon);53. tv.setCompoundDrawables(null,icon,null,null);54. /new一個Tag,保存這個任務(wù)的RecentTaskInfo和Intent55. RecentTagtag=newRecentTag();56. =info;57. ent=int

31、ent;58. tv.setTag(tag);59. tv.setVisibility(View.VISIBLE);60. tv.setPressed(false);61. tv.clearFocus();62. +index;63. 64. 65. 66. 67. ./無關(guān)緊要的代碼68. 這里的過程是:先用ActivityManager獲取RecentTasks并生成一個List,然后利用這個List為Dialog中的每個任務(wù)初始化,并生成對應(yīng)的信息RecentTag。需要注意的是,RecentTag中的Intent是從對應(yīng)任務(wù)的原始Intent復(fù)制過來的,這意味著那個原始Intent的一些Extra參數(shù)將會一并復(fù)制過來,我來舉個例子:比如我的App支持從第三方啟動,并且第三方要提供一個token,當(dāng)然這個token就以Extra參數(shù)的形式放進(jìn)Intent里,然后通過startActivity()啟動我的App;然后我的App根據(jù)這個token來處理,注意這里,當(dāng)我的App退出后,再從近期任務(wù)里啟動這個App,之

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論