對Android近期任務列表(Recent Applications)的簡單分析
https://www.cnblogs.com/coding-way/archive/2013/06/05/3118732.html
這裏的近期任務列表就是長按Home鍵出來的那個Dialog,裏麵放著近期打開過的應用,當然3.0以上係統的多任務切換鍵也是。
這個Dialog的實現在Android源碼的/frameworks/base/policy/src/com/android/internal/policy/impl/RecentApplicationsDialog.java中。
接下來就對這個源碼分析一下。
public class RecentApplicationsDialog extends Dialog implements OnClickListener { // Elements for debugging support // private static final String LOG_TAG = "RecentApplicationsDialog"; private static final boolean DBG_FORCE_EMPTY_LIST = false; static private StatusBarManager sStatusBar; private static final int NUM_BUTTONS = 8; private static final int MAX_RECENT_TASKS = NUM_BUTTONS * 2; // allow for some discards final TextView[] mIcons = new TextView[NUM_BUTTONS]; View mNoAppsText; IntentFilter mBroadcastIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); class RecentTag { ActivityManager.RecentTaskInfo info; Intent intent; } Handler mHandler = new Handler(); Runnable mCleanup = new Runnable() { public void run() { // dump extra memory we're hanging on to for (TextView icon: mIcons) { icon.setCompoundDrawables(null, null, null, null); icon.setTag(null); } } }; public RecentApplicationsDialog(Context context) { super(context, com.android.internal.R.style.Theme_Dialog_RecentApplications); } /** * We create the recent applications dialog just once, and it stays around (hidden) * until activated by the user. * * @see PhoneWindowManager#showRecentAppsDialog */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Context context = getContext(); if (sStatusBar == null) { sStatusBar = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE); } Window window = getWindow(); window.requestFeature(Window.FEATURE_NO_TITLE); window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); window.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); window.setTitle("Recents"); setContentView(com.android.internal.R.layout.recent_apps_dialog); final WindowManager.LayoutParams params = window.getAttributes(); params.width = WindowManager.LayoutParams.MATCH_PARENT; params.height = WindowManager.LayoutParams.MATCH_PARENT; window.setAttributes(params); window.setFlags(0, WindowManager.LayoutParams.FLAG_DIM_BEHIND); //默認顯示8個 mIcons[0] = (TextView)findViewById(com.android.internal.R.id.button0); mIcons[1] = (TextView)findViewById(com.android.internal.R.id.button1); mIcons[2] = (TextView)findViewById(com.android.internal.R.id.button2); mIcons[3] = (TextView)findViewById(com.android.internal.R.id.button3); mIcons[4] = (TextView)findViewById(com.android.internal.R.id.button4); mIcons[5] = (TextView)findViewById(com.android.internal.R.id.button5); mIcons[6] = (TextView)findViewById(com.android.internal.R.id.button6); mIcons[7] = (TextView)findViewById(com.android.internal.R.id.button7); mNoAppsText = findViewById(com.android.internal.R.id.no_applications_message); //關鍵在哪,你懂得... for (TextView b: mIcons) { b.setOnClickListener(this); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_TAB) { // Ignore all meta keys other than SHIFT. The app switch key could be a // fallback action chorded with ALT, META or even CTRL depending on the key map. // DPad navigation is handled by the ViewRoot elsewhere. final boolean backward = event.isShiftPressed(); final int numIcons = mIcons.length; int numButtons = 0; while (numButtons < numIcons && mIcons[numButtons].getVisibility() == View.VISIBLE) { numButtons += 1; } if (numButtons != 0) { int nextFocus = backward ? numButtons - 1 : 0; for (int i = 0; i < numButtons; i++) { if (mIcons[i].hasFocus()) { if (backward) { nextFocus = (i + numButtons - 1) % numButtons; } else { nextFocus = (i + 1) % numButtons; } break; } } final int direction = backward ? View.FOCUS_BACKWARD : View.FOCUS_FORWARD; if (mIcons[nextFocus].requestFocus(direction)) { mIcons[nextFocus].playSoundEffect( SoundEffectConstants.getContantForFocusDirection(direction)); } } // The dialog always handles the key to prevent the ViewRoot from // performing the default navigation itself. return true; } return super.onKeyDown(keyCode, event); } /** * Dismiss the dialog and switch to the selected application. */ public void dismissAndSwitch() { final int numIcons = mIcons.length; RecentTag tag = null; for (int i = 0; i < numIcons; i++) { if (mIcons[i].getVisibility() != View.VISIBLE) { break; } if (i == 0 || mIcons[i].hasFocus()) { tag = (RecentTag) mIcons[i].getTag(); if (mIcons[i].hasFocus()) { break; } } } if (tag != null) { switchTo(tag); } dismiss(); } /** * Handler for user clicks. If a button was clicked, launch the corresponding activity. */ public void onClick(View v) { for (TextView b: mIcons) { if (b == v) { RecentTag tag = (RecentTag)b.getTag(); switchTo(tag); break; } } dismiss(); } // private void switchTo(RecentTag tag) { if (tag.info.id >= 0) { // This is an active task; it should just go to the foreground. final ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); am.moveTaskToFront(tag.info.id, ActivityManager.MOVE_TASK_WITH_HOME); } else if (tag.intent != null) { tag.intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY | Intent.FLAG_ACTIVITY_TASK_ON_HOME); try { getContext().startActivity(tag.intent); } catch (ActivityNotFoundException e) { Log.w("Recent", "Unable to launch recent task", e); } } } /** * Set up and show the recent activities dialog. */ @Override public void onStart() { super.onStart(); reloadButtons(); if (sStatusBar != null) { sStatusBar.disable(StatusBarManager.DISABLE_EXPAND); } // receive broadcasts getContext().registerReceiver(mBroadcastReceiver, mBroadcastIntentFilter); mHandler.removeCallbacks(mCleanup); } /** * Dismiss the recent activities dialog. */ @Override public void onStop() { super.onStop(); if (sStatusBar != null) { sStatusBar.disable(StatusBarManager.DISABLE_NONE); } // stop receiving broadcasts getContext().unregisterReceiver(mBroadcastReceiver); mHandler.postDelayed(mCleanup, 100); } /** * Reload the 6 buttons with recent activities */ private void reloadButtons() { final Context context = getContext(); final PackageManager pm = context.getPackageManager(); final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); final List<ActivityManager.RecentTaskInfo> recentTasks = am.getRecentTasks(MAX_RECENT_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE); ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME) .resolveActivityInfo(pm, 0); IconUtilities iconUtilities = new IconUtilities(getContext()); // Performance note: Our android performance guide says to prefer Iterator when // using a List class, but because we know that getRecentTasks() always returns // an ArrayList<>, we'll use a simple index instead. int index = 0; int numTasks = recentTasks.size(); for (int i = 0; i < numTasks && (index < NUM_BUTTONS); ++i) { final ActivityManager.RecentTaskInfo info = recentTasks.get(i); // for debug purposes only, disallow first result to create empty lists if (DBG_FORCE_EMPTY_LIST && (i == 0)) continue; Intent intent = new Intent(info.baseIntent); if (info.origActivity != null) { intent.setComponent(info.origActivity); } // Skip the current home activity. if (homeInfo != null) { if (homeInfo.packageName.equals( intent.getComponent().getPackageName()) && homeInfo.name.equals( intent.getComponent().getClassName())) { continue; } } intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) | Intent.FLAG_ACTIVITY_NEW_TASK); final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0); if (resolveInfo != null) { final ActivityInfo activityInfo = resolveInfo.activityInfo; final String title = activityInfo.loadLabel(pm).toString(); Drawable icon = activityInfo.loadIcon(pm); if (title != null && title.length() > 0 && icon != null) { final TextView tv = mIcons[index]; tv.setText(title); icon = iconUtilities.createIconDrawable(icon); tv.setCompoundDrawables(null, icon, null, null); RecentTag tag = new RecentTag(); tag.info = info; tag.intent = intent; tv.setTag(tag); tv.setVisibility(View.VISIBLE); tv.setPressed(false); tv.clearFocus(); ++index; } } } // handle the case of "no icons to show" mNoAppsText.setVisibility((index == 0) ? View.VISIBLE : View.GONE); // hide the rest for (; index < NUM_BUTTONS; ++index) { mIcons[index].setVisibility(View.GONE); } } /** * This is the listener for the ACTION_CLOSE_SYSTEM_DIALOGS intent. It's an indication that * we should close ourselves immediately, in order to allow a higher-priority UI to take over * (e.g. phone call received). */ private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) { String reason = intent.getStringExtra(PhoneWindowManager.SYSTEM_DIALOG_REASON_KEY); if (! PhoneWindowManager.SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) { dismiss(); } } } }; } RecentApplicationsDialog.java完整源碼
從源碼可以看出,關鍵部分有三處。
一個很關鍵的內部類:
// 每個任務都包含一個Tag,這個Tag保存著這個App的一些非常有用的信息 class RecentTag { ActivityManager.RecentTaskInfo info; Intent intent; }
這個RecentTag保存在每個近期任務的圖標裏,並且保存著這個任務的原始信息。
剛啟動Dialog時對每個任務的初始化:
private void reloadButtons() { final Context context = getContext(); final PackageManager pm = context.getPackageManager(); final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); //拿到最近使用的應用的信息列表 final List<ActivityManager.RecentTaskInfo> recentTasks = am.getRecentTasks(MAX_RECENT_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE); //自製一個home activity info,用來區分 ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME) .resolveActivityInfo(pm, 0); IconUtilities iconUtilities = new IconUtilities(getContext()); int index = 0; int numTasks = recentTasks.size(); //開始初始化每個任務的信息 for (int i = 0; i < numTasks && (index < NUM_BUTTONS); ++i) { final ActivityManager.RecentTaskInfo info = recentTasks.get(i); //複製一個任務的原始Intent Intent intent = new Intent(info.baseIntent); if (info.origActivity != null) { intent.setComponent(info.origActivity); } //跳過home activity if (homeInfo != null) { if (homeInfo.packageName.equals( intent.getComponent().getPackageName()) && homeInfo.name.equals( intent.getComponent().getClassName())) { continue; } } intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) | Intent.FLAG_ACTIVITY_NEW_TASK); final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0); if (resolveInfo != null) { final ActivityInfo activityInfo = resolveInfo.activityInfo; final String title = activityInfo.loadLabel(pm).toString(); Drawable icon = activityInfo.loadIcon(pm); if (title != null && title.length() > 0 && icon != null) { final TextView tv = mIcons[index]; tv.setText(title); icon = iconUtilities.createIconDrawable(icon); tv.setCompoundDrawables(null, icon, null, null); //new一個Tag,保存這個任務的RecentTaskInfo和Intent RecentTag tag = new RecentTag(); tag.info = info; tag.intent = intent; tv.setTag(tag); tv.setVisibility(View.VISIBLE); tv.setPressed(false); tv.clearFocus(); ++index; } } } ...//無關緊要的代碼 }
這裏的過程是:先用ActivityManager獲取RecentTasks並生成一個List,然後利用這個List為Dialog中的每個任務初始化,並生成對應的信息RecentTag。
需要注意的是,RecentTag中的Intent是從對應任務的原始Intent複製過來的,這意味著那個原始Intent的一些Extra參數將會一並複製過來,
我來舉個例子:比如我的App支持從第三方啟動,並且第三方要提供一個token,當然這個token就以Extra參數的形式放進Intent裏,然後通過startActivity()啟動我的App;然後我的App根據這個token來處理,注意這裏,當我的App退出後,再從近期任務裏啟動這個App,之前的那個token還會傳遞給我的App,這裏就會出現錯誤了,原因就是上麵分析的。這就是為什麼從第三方跳轉的應用不會出現在近期任務的列表裏(比如點擊短信裏的url啟動一個瀏覽器,之後近期任務裏隻有短信app,不會出現瀏覽器app)。要想不出現在近期任務裏,可以給Intent設置FLAG_ACTIVITY_NO_HISTORY標誌。
響應每個任務的點擊事件:
private void switchTo(RecentTag tag) { if (tag.info.id >= 0) { // 這個Task沒有退出,直接移動到前台 final ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); am.moveTaskToFront(tag.info.id, ActivityManager.MOVE_TASK_WITH_HOME); } else if (tag.intent != null) { //task退出了的話,id為-1,則使用RecentTag中的Intent重新啟動 tag.intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY | Intent.FLAG_ACTIVITY_TASK_ON_HOME); try { getContext().startActivity(tag.intent); } catch (ActivityNotFoundException e) { Log.w("Recent", "Unable to launch recent task", e); } } }
如果該Task沒有退出,隻是切到後台,則切換到前台;如果已經退出,就要重新啟動了。
這裏的Intent就是之前說的,重複使用的舊Intent了,這裏注意,係統添加了FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY和FLAG_ACTIVITY_TASK_ON_HOME標誌,所以我們可以在App中通過判斷Intent的flag是否包含這兩個來判斷是否是從近期任務裏啟動的。注意FLAG_ACTIVITY_TASK_ON_HOME標誌是Api 11添加的,所以11一下的之判斷FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY就行了。
最後更新:2017-04-03 12:54:02