分析:
判断的是 Activity 是否位于栈顶 所以应该获得 Activity的管理器 ---- ActivityManager
ActivityManager am = (ActivityManager) mContext.getSystemService(ACTIVITY_SERVICE);
有了管理器 就要想 我们需要的是 运行时的栈里面的信息 所以用am去获得 RunningTasks
List<ActivityManager.RunningTaskInfo> runningTaskInfoList = am.getRunningTasks(1);
此处的参数可点击进去看源码:
/** * Return a list of the tasks that are currently running, with * the most recent being first and older ones after in order. Note that * "running" does not mean any of the task's code is currently loaded or * activity -- the task may have been frozen by the system, so that it * can be restarted in its previous state when next brought to the * foreground. * * <p><b>Note: this method is only intended for debugging and presenting * task management user interfaces</b>. This should never be used for * core logic in an application, such as deciding between different * behaviors based on the information found here. Such uses are * <em>not</em> supported, and will likely break in the future. For * example, if multiple applications can be actively running at the * same time, assumptions made about the meaning of the data here for * purposes of control flow will be incorrect.</p> * * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method * is no longer available to third party * applications: the introduction of document-centric recents means * it can leak person information to the caller. For backwards compatibility, * it will still retu rn a small subset of its data: at least the caller's * own tasks, and possibly some other tasks * such as home that are known to not be sensitive. * * @param maxNum The maximum number of entries to return in the list. The * actual number returned may be smaller, depending on how many tasks the * user has started. * * @return Returns a list of RunningTaskInfo records describing each of * the running tasks. */ @Deprecated public List<RunningTaskInfo> getRunningTasks(int maxNum) throws SecurityException { try { return ActivityManagerNative.getDefault().getTasks(maxNum, 0); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }然后进行一个判空String topActivityName = null; if (null != runningTaskInfoList){topActivityName = (runningTaskInfoList.get(0).topActivity).getShortClassName();}关于参数可点击看源码:// Positional Access Operations /** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException if the index is out of range * (<tt>index < 0 || index >= size()</tt>) */ E get(int index);0是得到集合中的 最前面的那个/** * The activity component at the top of the history stack of the task. * This is what the user is currently doing. */ public ComponentName topActivity;得到栈顶Activity的名字 (就可以 与当前的Activity的名字进行比对了 如果相同 则当前的Activity位于栈顶)/** * Return the class name of this component. */ public String getClassName() { return mClass; } /** * Return the class name, either fully qualified or in a shortened form * (with a leading '.') if it is a suffix of the package. */ public String getShortClassName() { if (mClass.startsWith(mPackage)) { int PN = mPackage.length(); int CN = mClass.length(); if (CN > PN && mClass.charAt(PN) == '.') { return mClass.substring(PN, CN); } } return mClass; }看了源码就知道 该使用哪个方法获得 名字!
private boolean isTaskTop(String currentActivityName){ LogUtils.e(mContext,"currentActivityName:"+currentActivityName); ActivityManager am = (ActivityManager) mContext.getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> runningTaskInfoList = am.getRunningTasks(1); String topActivityName = null; if (null != runningTaskInfoList){ topActivityName = (runningTaskInfoList.get(0).topActivity).getShortClassName(); } LogUtils.e(mContext,"topActivityName:"+topActivityName); if (TextUtils.isEmpty(topActivityName)) return false; if (TextUtils.isEmpty(currentActivityName)) throw new IllegalArgumentException(); if (topActivityName.contains(currentActivityName)){ return true; }else { return false; } }
10-10 14:27:56.056 23895-23895/myapp.first.myapplication E/MainActivity: currentActivityName:MainActivity 10-10 14:27:56.060 23895-23895/myapp.first.myapplication E/MainActivity: topActivityName:.ui.MainActivity 10-10 14:27:56.073 23895-23895/myapp.first.myapplication E/MainActivity: 位于栈顶