我尝试在带有 xposed 的挂钩方法中显示 AlertDialog。问题是方法在一个线程中运行,而这个线程在一个线程中运行,等等......
例如:活动 -> 线程 -> 线程 -> ... -> 函数
有没有办法显示我的 AlertDialog ?我有上下文,但由于钩子函数不在主线程中,所以没用。
编辑(一些代码):
public class Xposed implements IXposedHookLoadPackage {
private Context ctx;
private Queue<String> queue = new LinkedList<>();
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
if (!lpparam.packageName.equals("xxx.xxx.xxx")) {
return;
}
// Here I get the context from the static class extending Application
findAndHookMethod("xxx.xxx.xxx", lpparam.classLoader, "attachBaseContext", Context.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
XposedBridge.log("context modified");
ctx = (Context) param.args[0];
}
});
findAndHookMethod("com.xxx.xxx.xxx", lpparam.classLoader, "e", "com.xxx.xxx.xxx", String.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
if (!(param.args[1]).equals("xxxxxxxxxxxxxx")) {
return ;
}
XposedBridge.log("New element detected detected");
Object param = param.args[0];
Object info = callMethod(param, "q");
// Here, I want to show my alertdialog
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
}
}
};
// I get the classic error like what I can't modify the ui
// in a thread that has not called Looper.prepare()
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setMessage("Are you sure ?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener)
.show();
}
});
}
谢谢