标签:actual i++ 目的 similar ati 创建 this last state
一、背景
在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; } }
以下是一个反射的工具类,有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> * 当你须要訪问静态字段的时候本方法适合你, * 你还能够通过调用 {@link #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 * {@link 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); } /** * 让一个{@link 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; } } /** * {@inheritDoc} */ @Override public int hashCode() { return object.hashCode(); } /** * {@inheritDoc} */ @Override public boolean equals(Object obj) { if (obj instanceof Reflect) { return object.equals(((Reflect) obj).get()); } return false; } /** * {@inheritDoc} */ @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是处理咱们业务逻辑的函数。
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
标签:actual i++ 目的 similar ati 创建 this last state
原文地址:http://www.cnblogs.com/blfbuaa/p/7219711.html