对Android近期任务列表格Recent Applications地简单分析资料报告.docx
《对Android近期任务列表格Recent Applications地简单分析资料报告.docx》由会员分享,可在线阅读,更多相关《对Android近期任务列表格Recent Applications地简单分析资料报告.docx(15页珍藏版)》请在冰豆网上搜索。
![对Android近期任务列表格Recent Applications地简单分析资料报告.docx](https://file1.bdocx.com/fileroot1/2022-12/14/a8a26268-b728-4819-bcb2-ce1b60090cc4/a8a26268-b728-4819-bcb2-ce1b60090cc41.gif)
对Android近期任务列表格RecentApplications地简单分析资料报告
对Android近期任务列表(RecentApplications)的简单分析
分类:
Android开发2013-12-3111:
06 1599人阅读 评论(0) 收藏 举报
.cnblogs./coding-way/archive/2013/06/05/3118732.html
这里的近期任务列表就是长按Home键出来的那个Dialog,里面放着近期打开过的应用,当然3.0以上系统的多任务切换键也是。
这个Dialog的实现在Android源码的/frameworks/base/policy/src//android/internal/policy/impl/RecentApplicationsDialog.java中。
接下来就对这个源码分析一下。
[java] viewplaincopy
1.public class RecentApplicationsDialog extends Dialog implements OnClickListener {
2. // Elements for debugging support
3.// private static final String LOG_TAG = "RecentApplicationsDialog";
4. private static final boolean DBG_FORCE_EMPTY_LIST = false;
5.
6. static private StatusBarManager sStatusBar;
7.
8. private static final int NUM_BUTTONS = 8;
9. private static final int MAX_RECENT_TASKS = NUM_BUTTONS * 2; // allow for some discards
10.
11. final TextView[] mIcons = new TextView[NUM_BUTTONS];
12. View mNoAppsText;
13. IntentFilter mBroadcastIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
14.
15. class RecentTag {
16. ActivityManager.RecentTaskInfo info;
17. Intent intent;
18. }
19.
20. Handler mHandler = new Handler();
21. Runnable mCleanup = new Runnable() {
22. public void run() {
23. // dump extra memory we're hanging on to
24. for (TextView icon:
mIcons) {
25. icon.setCompoundDrawables(null, null, null, null);
26. icon.setTag(null);
27. }
28. }
29. };
30.
31. public RecentApplicationsDialog(Context context) {
32. super(context, .android.internal.R.style.Theme_Dialog_RecentApplications);
33.
34. }
35.
36. /**
37. * We create the recent applications dialog just once, and it stays around (hidden)
38. * until activated by the user.
39. *
40. * see PhoneWindowManager#showRecentAppsDialog
41. */
42. Override
43. protected void onCreate(Bundle savedInstanceState) {
44. super.onCreate(savedInstanceState);
45.
46. Context context = getContext();
47.
48. if (sStatusBar == null) {
49. sStatusBar = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);
50. }
51.
52. Window window = 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(.android.internal.R.layout.recent_apps_dialog);
60.
61. final WindowManager.LayoutParams params = 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. mIcons[0] = (TextView)findViewById(.android.internal.R.id.button0);
69. mIcons[1] = (TextView)findViewById(.android.internal.R.id.button1);
70. mIcons[2] = (TextView)findViewById(.android.internal.R.id.button2);
71. mIcons[3] = (TextView)findViewById(.android.internal.R.id.button3);
72. mIcons[4] = (TextView)findViewById(.android.internal.R.id.button4);
73. mIcons[5] = (TextView)findViewById(.android.internal.R.id.button5);
74. mIcons[6] = (TextView)findViewById(.android.internal.R.id.button6);
75. mIcons[7] = (TextView)findViewById(.android.internal.R.id.button7);
76. mNoAppsText = findViewById(.android.internal.R.id.no_applications_message);
77.
78. //关键在哪,你懂得...
79. for (TextView b:
mIcons) {
80. b.setOnClickListener(this);
81. }
82. }
83.
84. Override
85. public boolean onKeyDown(int keyCode, KeyEvent event) {
86. if (keyCode == KeyEvent.KEYCODE_TAB) {
87. // Ignore all meta keys other than SHIFT. The app switch key could be a
88. // fallback action chorded with ALT, META or even CTRL depending on the key map.
89. // DPad navigation is handled by the ViewRoot elsewhere.
90. final boolean backward = event.isShiftPressed();
91. final int numIcons = mIcons.length;
92. int numButtons = 0;
93. while (numButtons < numIcons && mIcons[numButtons].getVisibility() == View.VISIBLE) {
94. numButtons += 1;
95. }
96. if (numButtons !
= 0) {
97. int nextFocus = backward ?
numButtons - 1 :
0;
98. for (int i = 0; i < numButtons; i++) {
99. if (mIcons[i].hasFocus()) {
100. if (backward) {
101. nextFocus = (i + numButtons - 1) % numButtons;
102. } else {
103. nextFocus = (i + 1) % numButtons;
104. }
105. break;
106. }
107. }
108. final int direction = backward ?
View.FOCUS_BACKWARD :
View.FOCUS_FORWARD;
109. if (mIcons[nextFocus].requestFocus(direction)) {
110. mIcons[nextFocus].playSoundEffect(
111. SoundEffectConstants.getContantForFocusDirection(direction));
112. }
113. }
114.
115. // The dialog always handles the key to prevent the ViewRoot from
116. // performing the default navigation itself.
117. return true;
118. }
119.
120. return super.onKeyDown(keyCode, event);
121. }
122.
123. /**
124. * Dismiss the dialog and switch to the selected application.
125. */
126. public void dismissAndSwitch() {
127. final int numIcons = mIcons.length;
128. RecentTag tag = null;
129. for (int i = 0; i < numIcons; i++) {
130. if (mIcons[i].getVisibility() !
= View.VISIBLE) {
131. break;
132. }
133. if (i == 0 || mIcons[i].hasFocus()) {
134. tag = (RecentTag) mIcons[i].getTag();
135. if (mIcons[i].hasFocus()) {
136. break;
137. }
138. }
139. }
140. if (tag !
= null) {
141. switchTo(tag);
142. }
143. dismiss();
144. }
145.
146. /**
147. * Handler for user clicks. If a button was clicked, launch the corresponding activity.
148. */
149. public void onClick(View v) {
150. for (TextView b:
mIcons) {
151. if (b == v) {
152. RecentTag tag = (RecentTag)b.getTag();
153. switchTo(tag);
154. break;
155. }
156. }
157. dismiss();
158. }
159.
160. //
161. private void switchTo(RecentTag tag) {
162. if (tag.info.id >= 0) {
163. // This is an active task; it should just go to the foreground.
164. final ActivityManager am = (ActivityManager)
165. getContext().getSystemService(Context.ACTIVITY_SERVICE);
166. am.moveTaskToFront(tag.info.id, ActivityManager.MOVE_TASK_WITH_HOME);
167. } else if (tag.intent !
= null) {
168. tag.intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
169. | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
170. try {
171. getContext().startActivity(tag.intent);
172. } catch (ActivityNotFoundException e) {
173. Log.w("Recent", "Unable to launch recent task", e);
174. }
175. }
176. }
177.
178. /**
179. * Set up and show the recent activities dialog.
180. */
181. Override
182. public void onStart() {
183. super.onStart();
184. reloadButtons();
185. if (sStatusBar !
= null) {
186. sStatusBar.disable(StatusBarManager.DISABLE_EXPAND);
187. }
188.
189. // receive broadcasts
190. getContext().registerReceiver(mBroadcastReceiver, mBroadcastIntentFilter);
191.
192. mHandler.removeCallbacks(mCleanup);
193. }
194.
195. /**
196. * Dismiss the recent activities dialog.
197. */
198. Override
199. public void onStop() {
200. super.onStop();
201.
202. if (sStatusBar !
= null) {
203. sStatusBar.disable(StatusBarManager.DISABLE_NONE);
204. }
205.
206. // stop receiving broadcasts
207. getContext().unregisterReceiver(mBroadcastReceiver);
208.
209. mHandler.postDelayed(mCleanup, 100);
210. }
211.
212. /**
213. * Reload the 6 buttons with recent activities
214. */
215. private void reloadButtons() {
216.
217. final Context context = getContext();
218. final PackageManager pm = context.getPackageManager();
219. final ActivityManager am = (ActivityManager)
220. context.getSystemService(Context.ACTIVITY_SERVICE);
221. final List recentTasks =
222. am.getRecentTasks(MAX_RECENT_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
223.
224. ActivityInfo homeInfo =
225. new Intent(Intent.ACTION_MAIN).add