xposed框架下通过Toast显示任意Application包名

最近玩了玩xposed框架,首先简单的写了一个能够在打开任意应用时通过Toast输出它的包名的小module,稍微的记录一下,其他也没有什么代码,主要就是hook了Instumentation的callApplicationOnCreate方法。主要代码如下

package com.example.classbrick.qqbrick;

import android.app.Application;
import android.content.Context;
import android.widget.Toast;

import com.example.classbrick.qqbrick.utils.MyLog;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * Created by classbrick on 2017/5/30.
 */

public class main implements IXposedHookLoadPackage {
    private static final String TAG = "QQBrick.main";

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        final Class clazz = XposedHelpers.findClass("android.app.Instrumentation", null);
        XposedHelpers.findAndHookMethod(clazz, "callApplicationOnCreate", Application.class
                , 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);
                Context context = null;
                if(param.args[0] instanceof Application){
                    context = ((Application) param.args[0]).getApplicationContext();
                } else {
                    MyLog.i(TAG, "hook callApplicationOnCreate failed");
                    return;
                }
                Toast.makeText(context, context.getPackageName(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

你可能感兴趣的:(XPOSED,android,框架,xposed)