private void proxy(){        try{            //获取当前ActivityThread            Class ClassActivityThread=Class.forName("android.app.ActivityThread");            Method methodCurrentActivityThread=ClassActivityThread.getDeclaredMethod("currentActivityThread");            Object activityThread=methodCurrentActivityThread.invoke(null);            //获取当前ActivityThread的mInstrumentation字段            Field fieldmInstrumentation=ClassActivityThread.getDeclaredField("mInstrumentation");            fieldmInstrumentation.setAccessible(true);            Object mInstrumentation=fieldmInstrumentation.get(activityThread);            //构造LoadAPK 调用LoadedApk(ActivityThread, ApplicationInfo,            //                    CompatibilityInfo,ClassLoader,boolean,boolean,boolean)构造函数            Class ClassInstrumentation=Class.forName("android.app.Instrumentation");            Class ClassContextImpl= Class.forName("android.app.ContextImpl");            //loadAPK packageInfo            Class classLoadedApk= Class.forName("android.app.LoadedApk");            Class classCompatibilityInfo=Class.forName("android.content.res.CompatibilityInfo");            Field fieldDEFAULT_COMPATIBILITY_INFO=classCompatibilityInfo.getDeclaredField("DEFAULT_COMPATIBILITY_INFO");            Object DEFAULT_COMPATIBILITY_INFO=fieldDEFAULT_COMPATIBILITY_INFO.get(null);            UserHandle.class.getDeclaredMethod("myUserId").invoke(null);            Constructor constructor=classLoadedApk.getConstructor(ClassActivityThread, ApplicationInfo.class,                    classCompatibilityInfo,ClassLoader.class,boolean.class,boolean.class,boolean.class);            ApplicationInfo applicationInfo=this.getApplicationInfo();            applicationInfo.className="com.xx.xx.CustomApp";            Object loadAPK=constructor.newInstance(activityThread,applicationInfo,                    DEFAULT_COMPATIBILITY_INFO,this.getClassLoader(),false,true,false);            //执行创建Application            Method methodCreateAppContext=ClassContextImpl.getDeclaredMethod("createAppContext",ClassActivityThread,classLoadedApk);            methodCreateAppContext.setAccessible(true);            Object appContext =methodCreateAppContext.invoke(null,activityThread,loadAPK);            CustomApp globalApp= (CustomApp) ClassInstrumentation.getDeclaredMethod("newApplication",ClassLoader.class,String.class,                    Context.class).invoke(mInstrumentation,this.getClassLoader(),"com.xx.xx.CustomApp",appContext);            Field fieldAppication=classLoadedApk.getDeclaredField("mApplication");            fieldAppication.setAccessible(true);            fieldAppication.set(loadAPK,globalApp);            //调用onCreate()             globalApp.onCreate();            globalApp.setAttached(true);                    }catch (Exception e){            e.printStackTrace();        }        //globalApp.onCreate();    }

 

代码没有经过考验,仅供学习参考 

更多相关文章

  1. android API之KeyguardManager简介
  2. Android(安卓)调用Camera和相册
  3. Android(安卓)-- Activity官方文档简译
  4. Android腾讯微薄客户端开发十二:博主的偶像
  5. Android发短信程序
  6. Android(安卓)获取电池基本信息代码
  7. Android关于OnTouch 和OnClick同时调用冲突的解决方案
  8. Android与Javascript交互示例(一)
  9. 【工具类】Android获取平板物理唯一标识码

随机推荐

  1. Android 弧形列表转盘的实现(三),View跟随Re
  2. Android自定义View的基本步骤和使用自定
  3. android使用ImageView加载本地SdCard图片
  4. 观摩Android的多层框架体系
  5. Android客户端处理服务器端返回的Json格
  6. onTouchEvent、onClick和onLongClick的调
  7. android SharedPreference的简单使用(登陆
  8. ScrollView can host only one direct ch
  9. Android 内部存储相关的函数(getCacheDir,
  10. Android(安卓)Studio maven-metadata.xml