activity棧管理的3種方式
一、背景
在android開發過程最經常使用的組件非activity莫屬。
通過分析activity的各種跳轉,執行同學能夠分析用戶的各種行為。更重要的一點是在做插件化的過程中,我們經常會對activity進行各種反射,來實現各種需求。
二、實現
2.1 通過對"android.app.ActivityThread"進行反射。獲取android系統的activity棧
遍歷activity棧能夠得到當前應用中的全部存活的activity。
Log.e("Baseactivty", this.getClass().getName() +":oncreate"); Class activityThreadClass = null; try { activityThreadClass = Class.forName("android.app.ActivityThread"); Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null); Field activitiesField = activityThreadClass.getDeclaredField("mActivities"); activitiesField.setAccessible(true); Map activities = (Map) activitiesField.get(activityThread); int i = 0; for (Object activityRecord : activities.values()) { Class activityRecordClass = activityRecord.getClass(); Field activityField = activityRecordClass.getDeclaredField("activity"); activityField.setAccessible(true); Activity activity = (Activity) activityField.get(activityRecord); Log.e("activityThreadClass", "index:" + i + ",sum:" + activities.size()+ ", class name:" + activity.getClass().getName()); i++; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
當你在Activity的oncreate中調用上述方法時。你會發現activity列表activities中沒有當前的activity,這是由於當前activity還沒有被增加棧中。可是onResume中調用上面的函數。就發現當前activity已經被增加棧中。
2.2 重寫Instrumentation
監聽activity的聲明周期。現實自己想要的操作。
首先"繞莊"操作,即在當前project的src文件夾以下新建anroid.app包,然後聲明ActivityThread、Instrumentation、LoadedApk三個文件。
ActivityThread類
package android.app; import android.content.pm.ApplicationInfo; public final class ActivityThread { /** * NOTICE: 必須在UI線程調用本方法,否則返回NULL * * @return */ public static ActivityThread currentActivityThread() { return null; } public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai) { return null; } }
Instrumentation類
package android.app; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.os.IBinder; import android.view.KeyEvent; import android.view.MotionEvent; public class Instrumentation { public void onCreate(Bundle arguments) { } public void start() { } public void onStart() { } public boolean onException(Object obj, Throwable e) { return false; } public void sendStatus(int resultCode, Bundle results) { } public void finish(int resultCode, Bundle results) { } public void setAutomaticPerformanceSnapshots() { } public void startPerformanceSnapshot() { } public void endPerformanceSnapshot() { } public void onDestroy() { } public Context getContext() { return null; } public ComponentName getComponentName() { return null; } public Context getTargetContext() { return null; } public boolean isProfiling() { return false; } public void startProfiling() { } public void stopProfiling() { } public void setInTouchMode(boolean inTouch) { } public void waitForIdle(Runnable recipient) { } public void waitForIdleSync() { } public void runOnMainSync(Runnable runner) { } public Activity startActivitySync(Intent intent) { return null; } public void addMonitor(ActivityMonitor monitor) { } public Instrumentation.ActivityMonitor addMonitor(IntentFilter filter, ActivityResult result, boolean block) { return null; } public ActivityMonitor addMonitor(String cls, ActivityResult result, boolean block) { return null; } public boolean checkMonitorHit(ActivityMonitor monitor, int minHits) { return false; } public Activity waitForMonitor(ActivityMonitor monitor) { return null; } public Activity waitForMonitorWithTimeout(ActivityMonitor monitor, long timeOut) { return null; } public void removeMonitor(ActivityMonitor monitor) { } public boolean invokeMenuActionSync(Activity targetActivity, int requestCode, int flag) { return false; } public boolean invokeContextMenuAction(Activity targetActivity, int requestCode, int flag) { return false; } public void sendStringSync(String text) { } public void sendKeySync(KeyEvent event) { } public void sendKeyDownUpSync(int key) { } public void sendCharacterSync(int keyCode) { } public void sendPointerSync(MotionEvent event) { } public void sendTrackballEventSync(MotionEvent event) { } public Application newApplication(ClassLoader cl, String className, Context who) throws InstantiationException, IllegalAccessException, ClassNotFoundException { return null; } public void callApplicationOnCreate(Application app) { } public Activity newActivity(Class<?> clazz, Context who, IBinder token, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, Object lastNonConfigurationInstance) throws InstantiationException, IllegalAccessException { return null; } public Activity newActivity(ClassLoader cl, String className, Intent intent) throws InstantiationException, IllegalAccessException, ClassNotFoundException { return null; } public void callActivityOnCreate(Activity target, Bundle icicle) { } public void callActivityOnDestroy(Activity target) { } public void callActivityOnRestoreInstanceState(Activity target, Bundle savedInstanceState) { } public void callActivityOnPostCreate(Activity target, Bundle icicle) { } public void callActivityOnNewIntent(Activity target, Intent intent) { } public void callActivityOnStart(Activity target) { } public void callActivityOnRestart(Activity target) { } public void callActivityOnResume(Activity target) { } public void callActivityOnStop(Activity target) { } public void callActivityOnSaveInstanceState(Activity target, Bundle outState) { } public void callActivityOnPause(Activity target) { } public void callActivityOnUserLeaving(Activity target) { } public void startAllocCounting() { } public void stopAllocCounting() { } public Bundle getAllocCounts() { return null; } public Bundle getBinderCounts() { return null; } public UiAutomation getUiAutomation() { return null; } public ActivityResult execStartActivity(final Context who, final IBinder contextThread, final IBinder token, final Activity target, final Intent intent, final int requestCode) { return null; } public ActivityResult execStartActivity(final Context who, final IBinder contextThread, final IBinder token, final Activity target, final Intent intent, final int requestCode, final Bundle options) { return null; } public ActivityResult execStartActivity(final Context who, final IBinder contextThread, final IBinder token, final Fragment fragment, final Intent intent, final int requestCode) { return null; } public ActivityResult execStartActivity(final Context who, final IBinder contextThread, final IBinder token, final Fragment fragment, final Intent intent, final int requestCode, final Bundle options) { return null; } protected static final class ActivityMonitor { } public static final class ActivityResult { } }
LoadedApk類
package android.app; public class LoadedApk { public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) { return null; } public ClassLoader getClassLoader() { return null; } }
首先"繞莊"的目的是為繞過編譯過程,Instrumentation屬於內核實現類型,不能直接使用,否則會編譯出錯。
以下是一個反射的工具類,有lody大神設計
NULL類
/** * 用來表示null的類. * */ public class NULL { }
Reflect類
import java.lang.reflect.AccessibleObject; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; import android.annotation.SuppressLint; /** * 一個擁有流暢特性(Fluent-API)的反射工具類, * 使用起來就像直接調用一樣流暢易懂. * */ @SuppressLint("DefaultLocale") public class Reflect { private final Object object; private final boolean isClass; private Reflect(Class<?> type) { this.object = type; this.isClass = true; } private Reflect(Object object) { this.object = object; this.isClass = false; } /** * 依據指定的類名構建反射工具類 * * @param name 類的全名 * @return 反射工具類 * @throws 假設反射出現意外 * @see #on(Class) */ public static Reflect on(String name) throws ReflectException { return on(forName(name)); } /** * 從指定的類載入起尋找類,並構建反射工具類 * * @param name 類的全名 * @param classLoader 須要構建工具類的類的類載入器 * loaded. * @return 反射工具類 * @throws ReflectException 假設反射出現意外 * @see #on(Class) */ public static Reflect on(String name, ClassLoader classLoader) throws ReflectException { return on(forName(name, classLoader)); } /** * 依據指定的類構建反射工具類 * <p> * 當你須要訪問靜態字段的時候本方法適合你, * 你還能夠通過調用 [email protected] #create(Object...)} 創建一個對象. * * @param clazz 須要構建反射工具類的類 * @return 反射工具類 */ public static Reflect on(Class<?> clazz) { return new Reflect(clazz); } // --------------------------------------------------------------------- // 構造器 // --------------------------------------------------------------------- /** * Wrap an object. * <p> * Use this when you want to access instance fields and methods on any * [email protected] Object} * * @param object The object to be wrapped * @return A wrapped object, to be used for further reflection. */ public static Reflect on(Object object) { return new Reflect(object); } /** * [email protected] AccessibleObject}可訪問. * * @param accessible * @param <T> * @return */ public static <T extends AccessibleObject> T accessible(T accessible) { if (accessible == null) { return null; } if (accessible instanceof Member) { Member member = (Member) accessible; if (Modifier.isPublic(member.getModifiers()) && Modifier.isPublic(member.getDeclaringClass().getModifiers())) { return accessible; } } if (!accessible.isAccessible()) { accessible.setAccessible(true); } return accessible; } // --------------------------------------------------------------------- // Fluent Reflection API // --------------------------------------------------------------------- /** * 將給定字符串的開頭改為小寫. * * @param string * @return */ @SuppressLint("DefaultLocale") private static String property(String string) { int length = string.length(); if (length == 0) { return ""; } else if (length == 1) { return string.toLowerCase(); } else { return string.substring(0, 1).toLowerCase() + string.substring(1); } } private static Reflect on(Constructor<?
> constructor, Object... args) throws ReflectException { try { return on(accessible(constructor).newInstance(args)); } catch (Exception e) { throw new ReflectException(e); } } private static Reflect on(Method method, Object object, Object... args) throws ReflectException { try { accessible(method); if (method.getReturnType() == void.class) { method.invoke(object, args); return on(object); } else { return on(method.invoke(object, args)); } } catch (Exception e) { throw new ReflectException(e); } } /** * 取得內部維護的對象. */ private static Object unwrap(Object object) { if (object instanceof Reflect) { return ((Reflect) object).get(); } return object; } /** * 將Object數組轉換為其類型的數組. * 假設對象中包括null,我們用NULL.class取代. * * @see Object#getClass() */ private static Class<?
>[] types(Object... values) { if (values == null) { return new Class[0]; } Class<?>[] result = new Class[values.length]; for (int i = 0; i < values.length; i++) { Object value = values[i]; result[i] = value == null ?
NULL.class : value.getClass(); } return result; } /** * 取得一個類,此操作會初始化類的static區域. * * @see Class#forName(String) */ private static Class<?
> forName(String name) throws ReflectException { try { return Class.forName(name); } catch (Exception e) { throw new ReflectException(e); } } private static Class<?
> forName(String name, ClassLoader classLoader) throws ReflectException { try { return Class.forName(name, true, classLoader); } catch (Exception e) { throw new ReflectException(e); } } /** * 假設給定的Class是原始類型,那麽將其包裝為對象類型, * 否則返回本身. */ public static Class<?
> wrapper(Class<?> type) { if (type == null) { return null; } else if (type.isPrimitive()) { if (boolean.class == type) { return Boolean.class; } else if (int.class == type) { return Integer.class; } else if (long.class == type) { return Long.class; } else if (short.class == type) { return Short.class; } else if (byte.class == type) { return Byte.class; } else if (double.class == type) { return Double.class; } else if (float.class == type) { return Float.class; } else if (char.class == type) { return Character.class; } else if (void.class == type) { return Void.class; } } return type; } /** * 取得內部維護的實際對象 * * @param <T> * @return */ @SuppressWarnings("unchecked") public <T> T get() { return (T) object; } /** * 設置指定字段為指定值 * * @param name * @param value * @return * @throws ReflectException */ public Reflect set(String name, Object value) throws ReflectException { try { Field field = field0(name); field.setAccessible(true); field.set(object, unwrap(value)); return this; } catch (Exception e) { throw new ReflectException(e); } } /** * @param name * @param <T> * @return * @throws ReflectException */ public <T> T get(String name) throws ReflectException { return field(name).get(); } /** * 取得指定名稱的字段 * * @param name * @return * @throws ReflectException */ public Reflect field(String name) throws ReflectException { try { Field field = field0(name); return on(field.get(object)); } catch (Exception e) { throw new ReflectException(e); } } private Field field0(String name) throws ReflectException { Class<?> type = type(); // 先嘗試取得公有字段 try { return type.getField(name); } //此時嘗試非公有字段 catch (NoSuchFieldException e) { do { try { return accessible(type.getDeclaredField(name)); } catch (NoSuchFieldException ignore) { } type = type.getSuperclass(); } while (type != null); throw new ReflectException(e); } } /** * 取得一個Map,map中的key為字段名,value為字段相應的反射工具類 * * @return */ public Map<String, Reflect> fields() { Map<String, Reflect> result = new LinkedHashMap<String, Reflect>(); Class<?> type = type(); do { for (Field field : type.getDeclaredFields()) { if (!isClass ^ Modifier.isStatic(field.getModifiers())) { String name = field.getName(); if (!result.containsKey(name)) result.put(name, field(name)); } } type = type.getSuperclass(); } while (type != null); return result; } /** * 調用指定的無參數方法 * * @param name * @return * @throws androidx.pluginmgr.reflect.ReflectException */ public Reflect call(String name) throws ReflectException { return call(name, new Object[0]); } /** * 調用方法依據傳入的參數 * * @param name * @param args * @return * @throws androidx.pluginmgr.reflect.ReflectException */ public Reflect call(String name, Object... args) throws ReflectException { Class<?
>[] types = types(args); try { Method method = exactMethod(name, types); return on(method, object, args); } catch (NoSuchMethodException e) { try { Method method = similarMethod(name, types); return on(method, object, args); } catch (NoSuchMethodException e1) { throw new ReflectException(e1); } } } private Method exactMethod(String name, Class<?>[] types) throws NoSuchMethodException { Class<?> type = type(); try { return type.getMethod(name, types); } catch (NoSuchMethodException e) { do { try { return type.getDeclaredMethod(name, types); } catch (NoSuchMethodException ignore) { } type = type.getSuperclass(); } while (type != null); throw new NoSuchMethodException(); } } /** * 依據參數和名稱匹配方法,假設找不到方法, */ private Method similarMethod(String name, Class<?>[] types) throws NoSuchMethodException { Class<?> type = type(); for (Method method : type.getMethods()) { if (isSimilarSignature(method, name, types)) { return method; } } do { for (Method method : type.getDeclaredMethods()) { if (isSimilarSignature(method, name, types)) { return method; } } type = type.getSuperclass(); } while (type != null); throw new NoSuchMethodException("No similar method " + name + " with params " + Arrays.toString(types) + " could be found on type " + type() + "."); } private boolean isSimilarSignature(Method possiblyMatchingMethod, String desiredMethodName, Class<?
>[] desiredParamTypes) { return possiblyMatchingMethod.getName().equals(desiredMethodName) && match(possiblyMatchingMethod.getParameterTypes(), desiredParamTypes); } /** * 創建一個實例通過默認構造器 * * @return * @throws androidx.pluginmgr.reflect.ReflectException */ public Reflect create() throws ReflectException { return create(new Object[0]); } /** * 創建一個實例依據傳入的參數 * * @param args * @return * @throws ReflectException */ public Reflect create(Object... args) throws ReflectException { Class<?>[] types = types(args); try { Constructor<?> constructor = type().getDeclaredConstructor(types); return on(constructor, args); } catch (NoSuchMethodException e) { for (Constructor<?> constructor : type().getDeclaredConstructors()) { if (match(constructor.getParameterTypes(), types)) { return on(constructor, args); } } throw new ReflectException(e); } } /** * 創建一個動態代理依據傳入的類型. * 假設我們正在維護的是一個Map,那麽當調用出現異常時我們將從Map中取值. * * @param proxyType 須要動態代理的類型 * @return 動態代理生成的對象 */ @SuppressWarnings("unchecked") public <P> P as(Class<P> proxyType) { final boolean isMap = (object instanceof Map); final InvocationHandler handler = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String name = method.getName(); try { return on(object).call(name, args).get(); } catch (ReflectException e) { if (isMap) { Map<String, Object> map = (Map<String, Object>) object; int length = (args == null ?
0 : args.length); if (length == 0 && name.startsWith("get")) { return map.get(property(name.substring(3))); } else if (length == 0 && name.startsWith("is")) { return map.get(property(name.substring(2))); } else if (length == 1 && name.startsWith("set")) { map.put(property(name.substring(3)), args[0]); return null; } } throw e; } } }; return (P) Proxy.newProxyInstance(proxyType.getClassLoader(), new Class[]{proxyType}, handler); } /** * 檢查兩個數組的類型是否匹配,假設數組中包括原始類型,將它們轉換為相應的包裝類型. */ private boolean match(Class<?
>[] declaredTypes, Class<?>[] actualTypes) { if (declaredTypes.length == actualTypes.length) { for (int i = 0; i < actualTypes.length; i++) { if (actualTypes[i] == NULL.class) continue; if (wrapper(declaredTypes[i]).isAssignableFrom(wrapper(actualTypes[i]))) continue; return false; } return true; } else { return false; } } /** * [email protected]} */ @Override public int hashCode() { return object.hashCode(); } /** * [email protected]} */ @Override public boolean equals(Object obj) { if (obj instanceof Reflect) { return object.equals(((Reflect) obj).get()); } return false; } /** * [email protected]} */ @Override public String toString() { return object.toString(); } /** * 取得我們正在反射的對象的類型. * * @see Object#getClass() */ public Class<?> type() { if (isClass) { return (Class<?>) object; } else { return object.getClass(); } } }
ReflectException
/** * */ public class ReflectException extends RuntimeException { private static final long serialVersionUID = 663038727503637969L; public ReflectException(String message) { super(message); } public ReflectException(String message, Throwable cause) { super(message, cause); } public ReflectException() { super(); } public ReflectException(Throwable cause) { super(cause); } }
接下來是activityThread的代理類
import Reflect; import android.app.ActivityThread; import android.app.Application; import android.app.Instrumentation; public class DelegateActivityThread { private static DelegateActivityThread SINGLETOPN = new DelegateActivityThread(); private Reflect mActivityThreadReflect; public DelegateActivityThread() { mActivityThreadReflect = Reflect.on(ActivityThread.currentActivityThread()); } public static DelegateActivityThread getSingletion() { return SINGLETOPN; } public Application getInitialApplication() { return mActivityThreadReflect.get("mInitialApplication"); } public Instrumentation getInstrumentation() { return mActivityThreadReflect.get("mInstrumentation"); } public void setInstrumentation(Instrumentation newInstrumentation) { mActivityThreadReflect.set("mInstrumentation", newInstrumentation); } }
要在Instrumentation進行咱們自己操作的繼承類DelegateInstrumentation
import android.annotation.TargetApi; import android.app.Activity; import android.app.Application; import android.app.Fragment; import android.app.Instrumentation; import android.app.UiAutomation; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ActivityInfo; import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; public class DelegateInstrumentation extends Instrumentation { private Instrumentation mBase; /** * @param mBase 真正的Instrumentation */ public DelegateInstrumentation(Instrumentation mBase) { this.mBase = mBase; } @Override public void onCreate(Bundle arguments) { mBase.onCreate(arguments); } @Override public void start() { mBase.start(); } @Override public void onStart() { mBase.onStart(); } @Override public boolean onException(Object obj, Throwable e) { return mBase.onException(obj, e); } @Override public void sendStatus(int resultCode, Bundle results) { mBase.sendStatus(resultCode, results); } @Override public void finish(int resultCode, Bundle results) { mBase.finish(resultCode, results); } @Override public void setAutomaticPerformanceSnapshots() { mBase.setAutomaticPerformanceSnapshots(); } @Override public void startPerformanceSnapshot() { mBase.startPerformanceSnapshot(); } @Override public void endPerformanceSnapshot() { mBase.endPerformanceSnapshot(); } @Override public void onDestroy() { mBase.onDestroy(); } @Override public Context getContext() { return mBase.getContext(); } @Override public ComponentName getComponentName() { return mBase.getComponentName(); } @Override public Context getTargetContext() { return mBase.getTargetContext(); } @Override public boolean isProfiling() { return mBase.isProfiling(); } @Override public void startProfiling() { mBase.startProfiling(); } @Override public void stopProfiling() { mBase.stopProfiling(); } @Override public void setInTouchMode(boolean inTouch) { mBase.setInTouchMode(inTouch); } @Override public void waitForIdle(Runnable recipient) { mBase.waitForIdle(recipient); } @Override public void waitForIdleSync() { mBase.waitForIdleSync(); } @Override public void runOnMainSync(Runnable runner) { mBase.runOnMainSync(runner); } @Override public Activity startActivitySync(Intent intent) { return mBase.startActivitySync(intent); } @Override public void addMonitor(ActivityMonitor monitor) { mBase.addMonitor(monitor); } @Override public ActivityMonitor addMonitor(IntentFilter filter, ActivityResult result, boolean block) { return mBase.addMonitor(filter, result, block); } @Override public ActivityMonitor addMonitor(String cls, ActivityResult result, boolean block) { return mBase.addMonitor(cls, result, block); } @Override public boolean checkMonitorHit(ActivityMonitor monitor, int minHits) { return mBase.checkMonitorHit(monitor, minHits); } @Override public Activity waitForMonitor(ActivityMonitor monitor) { return mBase.waitForMonitor(monitor); } @Override public Activity waitForMonitorWithTimeout(ActivityMonitor monitor, long timeOut) { return mBase.waitForMonitorWithTimeout(monitor, timeOut); } @Override public void removeMonitor(ActivityMonitor monitor) { mBase.removeMonitor(monitor); } @Override public boolean invokeMenuActionSync(Activity targetActivity, int id, int flag) { return mBase.invokeMenuActionSync(targetActivity, id, flag); } @Override public boolean invokeContextMenuAction(Activity targetActivity, int id, int flag) { return mBase.invokeContextMenuAction(targetActivity, id, flag); } @Override public void sendStringSync(String text) { mBase.sendStringSync(text); } @Override public void sendKeySync(KeyEvent event) { mBase.sendKeySync(event); } @Override public void sendKeyDownUpSync(int key) { mBase.sendKeyDownUpSync(key); } @Override public void sendCharacterSync(int keyCode) { mBase.sendCharacterSync(keyCode); } @Override public void sendPointerSync(MotionEvent event) { mBase.sendPointerSync(event); } @Override public void sendTrackballEventSync(MotionEvent event) { mBase.sendTrackballEventSync(event); } @Override public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { return mBase.newApplication(cl, className, context); } @Override public void callApplicationOnCreate(Application app) { mBase.callApplicationOnCreate(app); } @Override public Activity newActivity(Class<?> clazz, Context context, IBinder token, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, Object lastNonConfigurationInstance) throws InstantiationException, IllegalAccessException { return mBase.newActivity(clazz, context, token, application, intent, info, title, parent, id, lastNonConfigurationInstance); } @Override public Activity newActivity(ClassLoader cl, String className, Intent intent) throws InstantiationException, IllegalAccessException, ClassNotFoundException { return mBase.newActivity(cl, className, intent); } @Override public void callActivityOnCreate(Activity activity, Bundle icicle) { mBase.callActivityOnCreate(activity, icicle); } @Override public void callActivityOnDestroy(Activity activity) { mBase.callActivityOnDestroy(activity); } @Override public void callActivityOnRestoreInstanceState(Activity activity, Bundle savedInstanceState) { mBase.callActivityOnRestoreInstanceState(activity, savedInstanceState); } @Override public void callActivityOnPostCreate(Activity activity, Bundle icicle) { mBase.callActivityOnPostCreate(activity, icicle); } @Override public void callActivityOnNewIntent(Activity activity, Intent intent) { mBase.callActivityOnNewIntent(activity, intent); } @Override public void callActivityOnStart(Activity activity) { mBase.callActivityOnStart(activity); } @Override public void callActivityOnRestart(Activity activity) { mBase.callActivityOnRestart(activity); } @Override public void callActivityOnResume(Activity activity) { mBase.callActivityOnResume(activity); } @Override public void callActivityOnStop(Activity activity) { mBase.callActivityOnStop(activity); } @Override public void callActivityOnSaveInstanceState(Activity activity, Bundle outState) { mBase.callActivityOnSaveInstanceState(activity, outState); } @Override public void callActivityOnPause(Activity activity) { mBase.callActivityOnPause(activity); } @TargetApi(Build.VERSION_CODES.CUPCAKE) @Override public void callActivityOnUserLeaving(Activity activity) { mBase.callActivityOnUserLeaving(activity); } @Override public void startAllocCounting() { mBase.startAllocCounting(); } @Override public void stopAllocCounting() { mBase.stopAllocCounting(); } @Override public Bundle getAllocCounts() { return mBase.getAllocCounts(); } @Override public Bundle getBinderCounts() { return mBase.getBinderCounts(); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @Override public UiAutomation getUiAutomation() { return mBase.getUiAutomation(); } @Override public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Fragment fragment, Intent intent, int requestCode) { doMyOperation(); return mBase.execStartActivity(who, contextThread, token, fragment, intent, requestCode); } @Override public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Fragment fragment, Intent intent, int requestCode, Bundle options) { doMyOperation(); return mBase.execStartActivity(who, contextThread, token, fragment, intent, requestCode, options); } @Override public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode) { doMyOperation(); return mBase.execStartActivity(who, contextThread, token, target, intent, requestCode); } @Override public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options) { doMyOperation(); return mBase.execStartActivity(who, contextThread, token, target, intent, requestCode, options); } public void doMyOperation() { } }
上面的doMyOperation是處理咱們業務邏輯的函數。
在Application裏面進行初始化的類ActivityManager;
import android.app.Instrumentation; import android.content.Context; import android.os.Looper; public class ActivityManager { private static ActivityManager SINGLETON; private Context mContext; private ActivityManager(Context context) { if (!isMainThread()) { return; } mContext = context; DelegateActivityThread delegateActivityThread = DelegateActivityThread.getSingletion(); Instrumentation originInstrumentation = delegateActivityThread.getInstrumentation(); if (!(originInstrumentation instanceof DelegateInstrumentation)) { DelegateInstrumentation delegateInstrumentation = new DelegateInstrumentation(originInstrumentation); delegateActivityThread.setInstrumentation(delegateInstrumentation); } } public static void init(Context context) { if (null != SINGLETON) { return; } SINGLETON = new ActivityManager(context); } private boolean isMainThread() { return Looper.getMainLooper() == Looper.myLooper(); } }
在Applicaiton進行初始化:
ActivityManager.init(this);
2.3 利用list自己管理的acitvity棧
無需多說。直接上代碼。
import java.util.ArrayList; import java.util.List; import android.app.Activity; /** * activity的管理棧,方便進行activity進行查找、處理 * * 眼下只適用於單線程 * * */ public class ActivityManager { private static ActivityManager SINGLETON = new ActivityManager(); private static List<Activity> mAcitivityList = new ArrayList<Activity>(); private ActivityManager() { if (null == mAcitivityList) { mAcitivityList = new ArrayList<Activity>(); } mAcitivityList.clear(); } public static ActivityManager getInstance() { if (null == SINGLETON) { SINGLETON = new ActivityManager(); } return SINGLETON; } /** * activity入棧 * * */ public void addActivity(Activity activity) { staticUserPage(activity); mAcitivityList.add(activity); } /** * activity出棧 * * */ public void popActivity(Activity activity) { if (null == mAcitivityList) { return; } int total = mAcitivityList.size(); if (total > 0) { mAcitivityList.remove(activity); } } /** * 獲取棧頂的activity * */ public Activity getTopActivity() { int total = mAcitivityList.size(); if (total > 0) { Activity currentActivity = mAcitivityList.get(total - 1); return currentActivity; } return null; } /** * 清空全部的activity * */ public void onExit() { if (null != mAcitivityList) { mAcitivityList.clear(); } mAcitivityList = null; } }
三、代碼project
項目下載地址:https://github.com/renxiaoys1/activitythread
activity棧管理的3種方式