全部
常见问题
产品动态
精选推荐

Java FieldUtils类使用实例

管理 管理 编辑 删除

实例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");
    }
}


CRMEB-慕白寒窗雪 最后编辑于2023-03-17 15:34:25

快捷回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
回复从新到旧

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} 作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest==1? '取消推荐': '推荐'}}
{{item.floor}}#
{{item.user_info.title}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

作者 管理员 企业

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}   {{itemc.ip_address}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
回复
回复
937
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

CRMEB-慕白寒窗雪 作者
社区运营专员---高冷のBoy | 呆萌のGirl

回答

1623

发布

1759

经验

42037

快速安全登录

使用微信扫码登录
{{item.label}} {{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
加精
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 开源下载 CRMEB官方论坛 帮助文档
返回顶部 返回顶部
CRMEB客服