下面列出了android.app.Application#getBaseContext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* onCallApplicationOnCreate
*
* @param app Application
*/
private void onCallApplicationOnCreate(Application app) {
String packageName = app.getPackageName();
boolean isPlugin = isPlugin(packageName);
if (!isPlugin) {
return;
}
// Begin:【FixBug】解决在中兴手机上找不到资源的问题,中兴部分手机的ROM上自己继承ContextImpl实现了一个AppContextImpl,
// 里面做一些BaseContext的复用,导致插件获取Resources时可能会取到宿主的。
try {
Class<?> clsCtxImpl = Class.forName("android.app.ContextImpl");
Object base = app.getBaseContext();
if (base.getClass() != clsCtxImpl) {
Constructor<?> cst = clsCtxImpl.getConstructor(clsCtxImpl);
Object impl = cst.newInstance(base);
JavaCalls.setField(app, "mBase", impl);
}
} catch (Exception e) {
if (DEBUG) {
e.printStackTrace();
}
if (ProxyEnvironment.hasInstance(app.getPackageName())) {
ReportManger.getInstance().onException(
ProxyEnvironment.getInstance(app.getPackageName()).getApplicationProxy(), packageName,
Util.getCallStack(e), ExceptionConstants.TJ_78730010);
}
}
replacePluginPackageName2Host(app);
replaceSystemServices(app);
replaceExternalDirs(app);
ProxyUtil.replaceSystemServices(app);
}
private static Object getPackageInfo(Application app) throws NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Context contextImpl = app.getBaseContext();
Field mPackageInfoField = contextImpl.getClass().getDeclaredField(
"mPackageInfo");
mPackageInfoField.setAccessible(true);
Object mPackageInfo = mPackageInfoField.get(contextImpl);
return mPackageInfo;
}
/**
* 得在插件相关的方法调用之前调用
*
* @param application application
*/
public static void init(Application application, HashMap<String, Integer> defaultList) {
if(defaultList == null){
mDefaultList = new HashMap<>();
}else{
mDefaultList = defaultList;
}
//初始化一些成员变量和加载已安装的插件
mPackageInfo = PluginUtil.getField(application.getBaseContext(), "mPackageInfo");
mBaseContext = application.getBaseContext();
mNowClassLoader = mBaseContext.getClassLoader();
mBaseClassLoader = mBaseContext.getClassLoader();
mNowResources = mBaseContext.getResources();
mBaseResources = mNowResources;
//更改系统的Instrumentation对象,以便创建插件的activity
Object mMainThread = PluginUtil.getField(mBaseContext, "mMainThread");
PluginUtil.setField(mMainThread, "mInstrumentation", new ZeusInstrumentation());
//创建插件的相关文件夹目录
createPath();
//加载已安装过的插件
loadInstalledPlugins();
//清除老版本的插件,最好放到软件退出时调用,防止让启动速度变慢
clearOldPlugin();
//安装内置插件
Thread initPluginThread = new Thread(new Runnable() {
@Override
public void run() {
installInitPlugins();
}
});
initPluginThread.setName("initPluginThread");
initPluginThread.start();
}
public SerieCsvExporterFactory(Application application) {
this(new SerieService(((KinoApplication) application).getDaoSession(), application.getBaseContext()));
}