从 Runnable 返回值 [英] Return value from Runnable

查看:131
本文介绍了从 Runnable 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几天的时间试图解决这个问题,但我不能,解决这个问题的方法是去 StackOverflow :D.碰巧我正在使用 libgdx(制作游戏的库)并在 Android 中查询代码是通过 Handler 类,运行一个 Runnable,我真的不知道它是如何工作的.

I have days trying to solve this but I can't, and the solution to this is to go to StackOverflow :D. Happens that I'm working with libgdx (library to make games) and to query code in Android is through the Handler class, running a Runnable and I don't really know how it works.

基本上我想要的是从 Runnable 中检索一个值.将 Handler 类与回调或类似的东西一起使用

不得不说,我对多线程编程不是很了解,在Handler类中看到了几个方法,但是看不懂是怎么工作的(消息、回调等)

I have to say that I don't really understand multithreading programming, and I saw several methods in Handler class, but I can't understand how it works (messaging, callbacks, etc.)

public class ActionResolverAndroid implements ActionResolver {
    Handler handler;
    Context context;

public ActionResolverAndroid(Context context) {
    handler = new Handler();
    this.context = context;
}

public boolean checkAndroidData(){
    final boolean[] result = {false};
    handler.post(new Runnable() {
            @Override
            public void run() {
                // Android code here
                // I need know the value of this variable
                result[0] = true;
            }
    });
    return result[0];
}

非常感谢阅读.加油

pd) 我不能使用 Runnable .join() 或 Callable<>

pd) I can't using Runnable .join() or Callable<>

推荐答案

当您将 Runnable 发布到 Handler 时,该代码将在 MainThread 中运行(您可以触摸您的视图).

When you post a Runnable to the Handler, that code will RUN in the MainThread (the one that you can touch your views).

正在做:

public boolean checkAndroidData(){
    final boolean[] result = {false};
    handler.post(new Runnable() {
            @Override
            public void run() {
                // Android code here
                // I need know the value of this variable
                result[0] = true;
            }
    });
    return result[0];
}

将使 result[0] 始终为 false,因为 Runnable 尚未运行.

Will make the result[0] being always false cause the Runnable would not runned yet.

您可以通知自己结论的方式是创建一个接口侦听器,您可以在 Runnable 结束时通知它.

The way you can notify yourself about the conclusion would be creating a Interface listener that you can notify when the Runnable ends.

考虑以下接口实现:

public interface Listener<T> {
    void on(T arg);
}

使用侦听器将等待侦听器中的响应而不是方法的返回值,因此上面的方法将类似于:

Working with a Listener would be waiting the response in the listener instead of the return value of a method, so the method above would be like:

public void checkAndroidData(Listener<Boolean> onCompleteListener){
    handler.post(new Runnable() {
            @Override
            public void run() {
                onCompleteListener.on(true);
            }
    });
}

要调用,您将传递一个实例并等待响应,例如:

And to call, you would pass a instance and wait for the response, like:

void onCreate(Bundle s){
    super.onCreate(s);
    checkAndroidData(new Listener<Boolean>(){
        public void on(Boolean result){
                Toast.makeText(result, Toast.LENGHT_LONG).show();
        }
    });
}

好吧,这是一个例子,在这种情况下,两个代码都将在 MainThread 中运行,这个例子不涉及多线程,而是关于如何监听您启动的事件.

Well, this is a example, and in this case both code will run in the MainThread, this example doesnt cover multithreading, but about how to listen to the event that you started.

但如果在该上下文中完成,该解决方案适用于多线程.

But the solution applies to multithreading if done in that context.

这篇关于从 Runnable 返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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