欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

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

    • 资源ID:4520050       资源大小:170.50KB        全文页数:11页
    • 资源格式: DOC        下载积分:8金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要8金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    对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

    注意事项

    本文(对Android近期任务列表(RecentApplications)的简单分析.doc)为本站会员(小**)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

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

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

    收起
    展开