我如何在 OnClick 方法中处理我的 Looper 以 运行 延迟函数?
How can i handle my Looper in an OnClick method to run a function with delay?
我希望能够 运行 通过单击事件中的循环程序在后台执行我的特定方法,这是正确的方法吗?
myThread = new LooperThread();
myThread.start();
upload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myThread.handler.post(new Runnable() {
@Override
public void run() {
//my methods
}
});
}
});
还有我的 Looper Class:
class LooperThread extends Thread {
Handler handler;
public void run() {
Looper.prepare();
handler = new Handler();
Looper.loop();
}
}
如果是,
问题是这样,我不明白为什么系统在我输入时不识别 "handler":"myThread.handler.post.." 和 运行 方法。
不然你能帮我做这个吗?
如果我在提问时犯了错误,我很抱歉,但这是我第一次来:)``
欢迎来到 Stack Overflow。
I would like to be able to run my specific method in background through a looper in an on click event, is this the right way to do this?
您的代码有效,但我不能说这是正确的方法。就像@tynn 提到的那样,HandlerThread
可能是更好的选择。
If it is, the problem is that in this way, i don't understand why the system don't recognize "handler" while i typing: "myThread.handler.post.." and run the methods.
Otherwise, can you help me on making this?
如果我理解你的问题,那就是访问问题。您的处理程序似乎无法访问,因为它被声明为包私有的。您可以通过这种方式让您的处理程序可见:
// Should this class be public or package-private?
public class LooperThread extends Thread {
private Handler handler;
public Handler getHandler() {
return handler;
}
// ...
}
您将能够像这样引用处理程序:
myThread.getHandler().post(...);
更新
您可以这样延迟 Runnable
执行:
// Delay execution of a Runnable task by 5000 milliseconds.
myThread.getHandler().postDelayed(myDelayedRunnable, 5000);
我希望能够 运行 通过单击事件中的循环程序在后台执行我的特定方法,这是正确的方法吗?
myThread = new LooperThread();
myThread.start();
upload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myThread.handler.post(new Runnable() {
@Override
public void run() {
//my methods
}
});
}
});
还有我的 Looper Class:
class LooperThread extends Thread {
Handler handler;
public void run() {
Looper.prepare();
handler = new Handler();
Looper.loop();
}
}
如果是, 问题是这样,我不明白为什么系统在我输入时不识别 "handler":"myThread.handler.post.." 和 运行 方法。
不然你能帮我做这个吗?
如果我在提问时犯了错误,我很抱歉,但这是我第一次来:)``
欢迎来到 Stack Overflow。
I would like to be able to run my specific method in background through a looper in an on click event, is this the right way to do this?
您的代码有效,但我不能说这是正确的方法。就像@tynn 提到的那样,HandlerThread
可能是更好的选择。
If it is, the problem is that in this way, i don't understand why the system don't recognize "handler" while i typing: "myThread.handler.post.." and run the methods.
Otherwise, can you help me on making this?
如果我理解你的问题,那就是访问问题。您的处理程序似乎无法访问,因为它被声明为包私有的。您可以通过这种方式让您的处理程序可见:
// Should this class be public or package-private?
public class LooperThread extends Thread {
private Handler handler;
public Handler getHandler() {
return handler;
}
// ...
}
您将能够像这样引用处理程序:
myThread.getHandler().post(...);
更新
您可以这样延迟 Runnable
执行:
// Delay execution of a Runnable task by 5000 milliseconds.
myThread.getHandler().postDelayed(myDelayedRunnable, 5000);