版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/y505772146/article/details/83146330
最近在使用Xposed hook Android App时,在做一个需求的时候有点懵:如何获取到android.app.NotificationManager中的Context呢?我的代码如下:
XposedHelpers.findAndHookMethod("android.app.NotificationManager",
lpparam.classLoader,
"notify",
String.class,
int.class,
Notification.class,
new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//获取context
super.beforeHookedMethod(param);
}
});
首先我一般使用如下的方法去获得Context:
XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
Context context = (Context) param.args[0];//在这里获取到Context
}
}
在这里我用attach方法获取到Context,但是在我的情况下,我们在notification里的参数是没有Context的,那我们怎么样去获取Context呢?
在这里我用Context context = AndroidAppHelper.currentApplication();获取到了一个Context。也就是说,我们有两种方式去获得Context,一种是hook生命周期函数或者参数带Context的函数,去获得Context;另一种就是直接使用AndroidAppHelper.currentApplication()去获取到Context。
这应该是Xposed最常用的一个需求了,希望自己能在使用Xposed的过程中更好的学习它。