对Android近期任务列表(RecentApplications)的简单分析.doc

上传人:小** 文档编号:4520050 上传时间:2021-09-26 格式:DOC 页数:11 大小:170.50KB
返回 下载 相关 举报
对Android近期任务列表(RecentApplications)的简单分析.doc_第1页
第1页 / 共11页
对Android近期任务列表(RecentApplications)的简单分析.doc_第2页
第2页 / 共11页
点击查看更多>>
资源描述

《对Android近期任务列表(RecentApplications)的简单分析.doc》由会员分享,可在线阅读,更多相关《对Android近期任务列表(RecentApplications)的简单分析.doc(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、,对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. publ

2、icclassRecentApplicationsDialogextendsDialogimplementsOnClickListener2. /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.RecentTaskInfoinf

4、o;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,com.android.internal.R.style.Theme_Dialog_RecentApplications);33. 34. 35. 36. /*37. *Wecreatetherecentapplicationsdialogjustonce,anditstaysaround(hidden)38. *untilactivatedbytheuser.39. *40. *seePhoneWindowManager#showRecentAppsDialog41. */42. Override43. protectedv

6、oidonCreate(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.FEATU

7、RE_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_dial

8、og);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. /默认显示

9、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=(TextV

10、iew)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.andro

11、id.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. /fallbac

12、kactionchordedwithALT,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_BACKW

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

15、rue;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;itshouldjustgototheforegrou

16、nd.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_ACTI

17、VITY_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. relo

18、adButtons();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. *Dismisstherecentactivitiesd

19、ialog.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.

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

21、alListrecentTasks=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();2

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

23、extViewtv=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)

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. *Thisisthelistenerforth

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

26、ntintent)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. ;3

27、07. 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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁