Xposed框架之模块安装验证方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21004057/article/details/52949047
最近在开发关于xposed方面的应用的时候,需要进行模块验证。我们知道xposed框架可以好hook应用的方法。但是在xposed的入口函数内是得不到Context对象的,之前想在
handleLoadPackage方法内进行广播,然后在广播内修改一下SharedPreferences的键值来判断模块是否安装,因为得不到Context对象,所以没法用广播。后来想到2个方法。
方法一:在主Activity中添加一个需要被hook掉的函数如下:
 
 
 
 
//用于判断模块是否安装
	public static  String getResult(){
		return "未安装";
	}

因为是默认的未安装,我们hook一下这个函数,令其返回值为已安装。如果hook成功,则代表已经安装,否则就没安装。
代码
 
 
          XSharedPreferences xpre = new XSharedPreferences("com.mero.wyt_register",Config.ID);
            final Class<?> thiz = XposedHelpers.findClass("com.mero.wyt_register.MainActivity",lpparam.classLoader);
            try{
                hookMethod(thiz,"getResult","已安装");
            }catch (Exception e){
                e.printStackTrace();
           

hookMethod代码如下:
    private void hookMethod(final Class<?> clz, String methodName,final String result) {
        XposedHelpers.findAndHookMethod(clz,methodName,new Object[]{
                new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                        super.beforeHookedMethod(param);

                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                        super.afterHookedMethod(param);
                        param.setResult(result);
                        XposedBridge.log("正在测试beforeHookedMethod");
                    }
                }
        });

方法二:获取Context对象。
我们可以获取全局的Context对象。首先,我们新建立一个MyApplication的类继承自Application类。
public class MyApplication extends Application {
    public static MyApplication instance;
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }
    public static MyApplication getMyApplication(){
        return instance;
    }
}

另外在配置文件中添加:Android:name = ".MyApplication";代码演示如下:
  <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar">

同样我们hook掉getMyApplication函数,在下面函数中拿到Context对象。
   protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                        super.afterHookedMethod(param);
                        param.setResult(result);
                        XposedBridge.log("正在测试beforeHookedMethod");
                        Context context = (Context) param.thisObject;
                    }
                }

记得把Context对象改成全局变量。这样就可以发送广播了。在广播里利用SharedPreferences对象储存一个字符串。随机什么都行。然后再在验证模块安装的地方得到这个字符串,判断是否与默认的也就是SharedPrefences对象
默认的getString(key,value);也就是这个value,假如你在这得到的数值和广播中发送的不一样,那么就代表hook不成功,也就是说你的模块还没有安装。

猜你喜欢

转载自blog.csdn.net/qq_21004057/article/details/52949047