通过在线程中运行的xposed在钩子方法中显示对话框 [英] Show dialog in hooked method by xposed running in thread

查看:341
本文介绍了通过在线程中运行的xposed在钩子方法中显示对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在具有xposed的挂钩方法中显示AlertDialog. 问题在于该方法正在线程中运行,而该线程正在线程中运行,等等.

I try to show an AlertDialog within a hooked method with xposed. The problem is that the method is running in a threads, and this thread is running in a thread, etc...

例如: 活动->线程->线程-> ...->函数

For example : Activity -> thread -> thread -> ... -> function

是否可以显示我的AlertDialog?我有Context,但是由于该钩子函数不在主线程中,因此它是无用的.

Is there a way to show my AlertDialog ? I have the Context, but since the hooked function is not in the main thread, it is useless.

编辑(某些代码):

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();
        }
    });
}

谢谢

推荐答案

您始终可以保留当前活动的引用(

You can always keep a reference of the current Activity (android.app.Instrumentation.newActivity). It goes something like this:

Class<?> instrumentation = XposedHelpers.findClass(
                "android.app.Instrumentation", lpparam.classLoader);

Method method = instrumentation.getMethod("newActivity",
                 ClassLoader.class, String.class, Intent.class);

XposedBridge.hookMethod(method, iHook);

在这种情况下,ihook将是您的一个钩子,它仅静态地存储当前的Activity,因此无论您身在何处,都可以调用runOnUiThread:

In this case the ihook would be a hook of yours that just stores the current Activity statically so you can invoke the runOnUiThread from wherever you are:

public class ActivityHook extends XC_MethodHook{

    /* Assure latest read of write */
    private static volatile Activity _currentActivity = null;

    public static Activity getCurrentActivity() {
        return _currentActivity;
    }

    @Override
    protected Object afterHookedMethod(MethodHookParam param)
            throws Throwable {
        _currentActivity = (Activity) param.getResult();
    }
}

那么您将可以在任何地方做

Then you will be able to do from anywhere:

ActivityHook.getCurrentActivity().runOnUiThread(...);

祝你好运!

这篇关于通过在线程中运行的xposed在钩子方法中显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆