java 从 Runnable 返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32771412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return value from Runnable
提问by josemwarrior
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 Handlerclass, running a Runnable and I don't really know how it works.
我有几天的时间试图解决这个问题,但我不能,解决这个问题的方法是去 StackOverflow :D。碰巧我正在使用 libgdx(制作游戏的库)并通过Handler类在 Android 中查询代码,运行 Runnable,但我真的不知道它是如何工作的。
Basically what I want is to retrieve a value from Runnable. Using the Handler class with callbacks or something like that
基本上我想要的是从 Runnable 中检索一个值。将 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.)
不得不说,我不是很懂多线程编程,在Handler类中看到了几个方法,但是看不懂是怎么工作的(消息、回调等)
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];
}
Thanks a lot for reading. cheer
非常感谢阅读。欢呼
pd) I can't using Runnable .join() or Callable<>
pd) 我不能使用 Runnable .join() 或 Callable<>
回答by Marcos Vasconcelos
When you post a Runnable to the Handler, that code will RUN in the MainThread (the one that you can touch your views).
当您将 Runnable 发布到 Handler 时,该代码将在 MainThread 中运行(您可以触摸您的视图)。
doing:
正在做:
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];
}
Will make the result[0] being always false cause the Runnable would not runned yet.
将使 result[0] 始终为假,因为 Runnable 尚未运行。
The way you can notify yourself about the conclusion would be creating a Interface listener that you can notify when the Runnable ends.
您可以通知自己有关结论的方式是创建一个接口侦听器,您可以在 Runnable 结束时通知它。
Consider the following interface implementation:
考虑以下接口实现:
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();
}
});
}
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.
嗯,这是一个例子,在这种情况下,两个代码都将在 MainThread 中运行,这个例子不涉及多线程,而是关于如何监听您启动的事件。
But the solution applies to multithreading if done in that context.
但如果在该上下文中完成,该解决方案适用于多线程。
回答by a person
You can use a callback:
您可以使用回调:
public interface RunnableListener
{
void onResult(boolean[] result);
}
// a field in your class
private RunnableListener runnableListener;
private void someMethod()
{
new Handler().post(new Runnable()
{
@Override public void run()
{
runnableListener.onResult(new boolean[]{true});
}
});
}