android.app.Instrumentation

这个类主要是用来管理activity的生命周期的

    public DemoApp() {
        super();
        //创建自己的instamatation
        try {
            Class atClazz = Class.forName("android.app.ActivityThread");
            Method method = atClazz.getDeclaredMethod("currentActivityThread", null);
            Object invoke = method.invoke(null, null);
            Field field = invoke.getClass().getDeclaredField("mInstrumentation");
            field.setAccessible(true);
            field.set(invoke,new DemoInstrumentation());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

class DemoInstrumentation extends Instrumentation{

    private static final String LOG_TAG = "DemoInstrumentation_TAG";

    @Override
    public void callApplicationOnCreate(Application app) {
        super.callApplicationOnCreate(app);
        Log.i(LOG_TAG,"callApplicationOnCreate  "+app);
    }

    @Override
    public void runOnMainSync(Runnable runner) {
        super.runOnMainSync(runner);
        Log.i(LOG_TAG,"runOnMainSync "+runner);
    }

    @Override
    public Activity startActivitySync(Intent intent) {
        Log.i(LOG_TAG,"startActivitySync   "+intent);
        return super.startActivitySync(intent);
    }

    @Override
    public void sendStringSync(String text) {
        super.sendStringSync(text);
        Log.i(LOG_TAG,"sendStringSync  "+text);
    }

    @Override
    public void sendKeySync(KeyEvent event) {
        super.sendKeySync(event);
        Log.i(LOG_TAG,"sendKeySync  "+event);
    }

    @Override
    public void sendKeyDownUpSync(int key) {
        super.sendKeyDownUpSync(key);
        Log.i(LOG_TAG,"sendKeyDownUpSync  "+key);
    }

    @Override
    public void sendPointerSync(MotionEvent event) {
        super.sendPointerSync(event);
        Log.i(LOG_TAG,"sendPointerSync  "+event);
    }

    @Override
    public Activity newActivity(ClassLoader cl, String className, Intent intent) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        Log.i(LOG_TAG,"newActivity "+cl+":"+className+":"+intent);
        return super.newActivity(cl, className, intent);
    }

    @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 {
        Log.i(LOG_TAG,"newActivity "+clazz+":"+context+":"+application);
        return super.newActivity(clazz, context, token, application, intent, info, title, parent, id, lastNonConfigurationInstance);
    }

    @Override
    public void callActivityOnCreate(Activity activity, Bundle icicle) {
        super.callActivityOnCreate(activity, icicle);
        Log.i(LOG_TAG,"callActivityOnCreate "+activity+"  "+icicle);
    }

    @Override
    public void callActivityOnCreate(Activity activity, Bundle icicle, PersistableBundle persistentState) {
        super.callActivityOnCreate(activity, icicle, persistentState);
        Log.i(LOG_TAG,"callActivityOnCreate "+activity+"  "+icicle+ "  "+persistentState);
    }

    @Override
    public void callActivityOnDestroy(Activity activity) {
        super.callActivityOnDestroy(activity);
        Log.i(LOG_TAG,"callActivityOnDestroy  "+activity);
    }

    @Override
    public void callActivityOnNewIntent(Activity activity, Intent intent) {
        super.callActivityOnNewIntent(activity, intent);
        Log.i(LOG_TAG,"callActivityOnNewIntent  "+activity+"  "+intent);
    }

    @Override
    public void callActivityOnStart(Activity activity) {
        super.callActivityOnStart(activity);
        Log.i(LOG_TAG,"callActivityOnStart  "+activity);
    }

    @Override
    public void callActivityOnResume(Activity activity) {
        super.callActivityOnResume(activity);
        Log.i(LOG_TAG,"callActivityOnResume  "+activity);
    }

    @Override
    public void callActivityOnStop(Activity activity) {
        super.callActivityOnStop(activity);
        Log.i(LOG_TAG,"callActivityOnStop  "+activity);
    }
}

Code in ActivityThread::main

    Instrumentation mInstrumentation;
    public static void main(String[] args) {
        SamplingProfilerIntegration.start();

        // CloseGuard defaults to true and can be quite spammy.  We
        // disable it here, but selectively enable it later (via
        // StrictMode) on debug builds, but using DropBox, not logs.
        CloseGuard.setEnabled(false);

        Environment.initForCurrentUser();

        // Set the reporter for event logging in libcore
        EventLogger.setReporter(new EventLoggingReporter());

        Security.addProvider(new AndroidKeyStoreProvider());

        Process.setArgV0("");

        Looper.prepareMainLooper();


        #########################################
             ActivityThread的一个成员变量
        #########################################
        ActivityThread thread = new ActivityThread();
        thread.attach(false);
        #########################################
        #########################################

        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }

        AsyncTask.init();

        if (false) {
            Looper.myLooper().setMessageLogging(new
                    LogPrinter(Log.DEBUG, "ActivityThread"));
        }

        Looper.loop();

        throw new RuntimeException("Main thread loop unexpectedly exited");
    }
}

Nothing is certain in this life. The only thing i know for sure is that. I love you and my life. That is the only thing i know. have a good day

:)

你可能感兴趣的:(android.app.Instrumentation)