实例1: fixBaseContextImplOpsPackage
import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
private void fixBaseContextImplOpsPackage(Context context) throws IllegalAccessException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 && context != null && !TextUtils.equals(context.getPackageName(), mHostContext.getPackageName())) {
Context baseContext = context;
Class clazz = baseContext.getClass();
Field mOpPackageName = FieldUtils.getDeclaredField(clazz, "mOpPackageName", true);
if (mOpPackageName != null) {
Object valueObj = mOpPackageName.get(baseContext);
if (valueObj instanceof String) {
String opPackageName = ((String) valueObj);
if (!TextUtils.equals(opPackageName, mHostContext.getPackageName())) {
mOpPackageName.set(baseContext, mHostContext.getPackageName());
Log.i(TAG, "fixBaseContextImplOpsPackage OK!Context=%s,", baseContext);
}
}
}
}
}
实例2: fixBaseContextImplContentResolverOpsPackage
import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
private void fixBaseContextImplContentResolverOpsPackage(Context context) throws IllegalAccessException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 && context != null && !TextUtils.equals(context.getPackageName(), mHostContext.getPackageName())) {
Context baseContext = context;
Class clazz = baseContext.getClass();
Field mContentResolver = FieldUtils.getDeclaredField(clazz, "mContentResolver", true);
if (mContentResolver != null) {
Object valueObj = mContentResolver.get(baseContext);
if (valueObj instanceof ContentResolver) {
ContentResolver contentResolver = ((ContentResolver) valueObj);
Field mPackageName = FieldUtils.getDeclaredField(ContentResolver.class, "mPackageName", true);
Object mPackageNameValueObj = mPackageName.get(contentResolver);
if (mPackageNameValueObj != null && mPackageNameValueObj instanceof String) {
String packageName = ((String) mPackageNameValueObj);
if (!TextUtils.equals(packageName, mHostContext.getPackageName())) {
mPackageName.set(contentResolver, mHostContext.getPackageName());
Log.i(TAG, "fixBaseContextImplContentResolverOpsPackage OK!Context=%s,contentResolver=%s", baseContext, contentResolver);
}
}
}
}
}
}
实例3: setIntentClassLoader
import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
private void setIntentClassLoader(Intent intent, ClassLoader classLoader) {
try {
Bundle mExtras = (Bundle) FieldUtils.readField(intent, "mExtras");
if (mExtras != null) {
mExtras.setClassLoader(classLoader);
} else {
Bundle value = new Bundle();
value.setClassLoader(classLoader);
FieldUtils.writeField(intent, "mExtras", value);
}
} catch (Exception e) {
} finally {
intent.setExtrasClassLoader(classLoader);
}
}
实例4: beforeInvoke
import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
@Override
protected boolean beforeInvoke(Object receiver, Method method, Object[] args) throws Throwable {
//这里适配在android 5.0的机器上无法现实toast的问题。但是我也不知道还有那些机器需要这样做。
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
int index = 1;
if (args != null && args.length > index) {
Object obj = args[index];
View view = (View) FieldUtils.readField(obj, "mView");
View nextView = (View) FieldUtils.readField(obj, "mNextView");
if (nextView != null) {
FieldUtils.writeField(nextView, "mContext", mHostContext);
}
if (view != null) {
FieldUtils.writeField(view, "mContext", mHostContext);
}
}
}
return super.beforeInvoke(receiver, method, args);
}
实例5: onInstall
import com.morgoo.droidplugin.reflect.FieldUtils; //导入依赖的package包/类
@Override
protected void onInstall(ClassLoader classLoader) throws Throwable {
Object target = ActivityThreadCompat.currentActivityThread();
Class ActivityThreadClass = ActivityThreadCompat.activityThreadClass();
/*替换ActivityThread.mInstrumentation,拦截组件调度消息*/
Field mInstrumentationField = FieldUtils.getField(ActivityThreadClass, "mInstrumentation");
Instrumentation mInstrumentation = (Instrumentation) FieldUtils.readField(mInstrumentationField, target);
if (!PluginInstrumentation.class.isInstance(mInstrumentation)) {
PluginInstrumentation pit = new PluginInstrumentation(mHostContext, mInstrumentation);
pit.setEnable(isEnable());
mPluginInstrumentations.add(pit);
FieldUtils.writeField(mInstrumentationField, target, pit);
Log.i(TAG, "Install Instrumentation Hook old=%s,new=%s", mInstrumentationField, pit);
} else {
Log.i(TAG, "Instrumentation has installed,skip");
}
}