我要设置从GUI线程HandlerThread。然后,一段时间后,点击在GUI上的一个按钮时,它运行callHello(),然后将消息发送到驻留在非GUI线程,异步方式记录的Hello World在HelloLogger对象。我已经尝试了一些事情,一些无限期地阻塞,一些从来没有收到消息,等等等等。code以下或多或少尽可能靠近我有,请可能有人修改它的工作?
公共类HandlerThreadExample {
私人MyHandlerThread mMyHandlerThread;
私人活套mLooper;
私人处理器mHandler;
公共HandlerThreadExample(){
mMyHandlerThread =新MyHandlerThread();
mMyHandlerThread.start();
mLooper = mMyHandlerThread.getLooper();
}
公共无效callHello(){
mHandler.sendEmptyMessage(1);
}
私有类MyHandlerThread扩展HandlerThread {
私人HelloLogger mHelloLogger;
私人处理器mHandler;
公共MyHandlerThread(){
超(以下简称MyHandlerThread线,HandlerThread.NORM_PRIORITY);
}
公共无效的run(){
mHelloLogger =新HelloLogger();
mHandler =新的处理程序(getLooper()){
公共无效的handleMessage(信息MSG){
mHelloLogger.logHello();
}
};
super.run();
}
}
私有类HelloLogger {
公共HelloLogger(){
}
公共无效logHello(){
Log.d(HandlerThreadExample,Hello World的);
}
}
}
找到最好的例子:
HandlerThread测试 How创建一个弯针线,然后立即发送邮件? 异步与处理器 通话 HandlerThread VS执行人 - 当一个比较合适的比其他 ? Best使用HandlerThread比其他类似的类 的Android HandlerThread HandlerThread例子 的Android版本:主要和工人之间传递数据螺纹 的Java花样 使用活动线程队列和处理程序类 发送线程之间的消息 简介活套和处理程序 developer.android:指定code到上一个运行螺纹至少现在我可以关闭这该死的标签
解决方案提供者的帮助下,从pskink
公共类HandlerThreadExample2 {
私有静态诠释MSG_START_HELLO = 0;
私有静态诠释MSG_HELLO_COMPLETE = 1;
私人HandlerThread羟色胺;
私人处理器mHtHandler;
私人处理器mUiHandler;
私人布尔helloReady = FALSE;
公共HandlerThreadExample2(){
HT =新HandlerThread(新思路);
ht.start();
Log.d(App.TAG,用户界面:处理器线程启动);
mUiHandler =新的处理程序(){
公共无效的handleMessage(信息MSG){
如果(msg.what == MSG_HELLO_COMPLETE){
Log.d(App.TAG,UI线程:接受睡眠完成通知);
helloReady = TRUE; }
}
};
mHtHandler =新的处理程序(ht.getLooper()){
公共无效的handleMessage(信息MSG){
如果(msg.what == MSG_START_HELLO){
Log.d(中的App.TAG的handleMessage+ msg.what + Thread.currentThread()+现在睡);
尝试 {
视频下载(2000);
}赶上(InterruptedException异常E){
e.printStackTrace();
}
Log.d(App.TAG,醒来后,通知UI线程......);
mUiHandler.sendEmptyMessage(MSG_HELLO_COMPLETE);
}
}
};
}
公共无效sendLongHello(){
如果(helloReady){
Log.d(App.TAG,送你好+ Thread.currentThread());
mHtHandler.sendEmptyMessage(MSG_START_HELLO);
helloReady = FALSE;
} 其他 {
Log.e(App.TAG,不能做招呼还没有 - 没有准备好);
}
}
}
解决方案
所以这是工作的例子:
HandlerThread HT =新HandlerThread(MySuperAwesomeHandlerThread);
ht.start();
处理程序H =新的处理程序(ht.getLooper()){
公共无效的handleMessage(信息MSG){
Log.d(TAG,中,的handleMessage+ msg.what + Thread.currentThread());
};
};
的for(int i = 0;我小于5;我++){
Log.d(中的标签,送+ I + Thread.currentThread());
h.sendEmptyMessageDelayed(I,3000 + I * 1000);
}
更新
请两类领域:
处理程序mHtHandler;
处理器mUiHandler;
和尝试这个办法:
HandlerThread HT =新HandlerThread(MySuperAwsomeHandlerThread);
ht.start();
回调回调=新的回调(){
@覆盖
公共布尔的handleMessage(信息MSG){
如果(msg.what == 0){
Log.d(TAG,得到了meaasage在,现在睡......+ Thread.currentThread()+);
尝试 {
视频下载(2000);
}赶上(InterruptedException异常E){
e.printStackTrace();
}
Log.d(TAG,醒来后,通知UI线程......);
mUiHandler.sendEmptyMessage(1);
} 其他
如果(msg.what == 1){
Log.d(TAG,得到了一个通知中的+ Thread.currentThread());
}
返回false;
}
};
mHtHandler =新的处理程序(ht.getLooper(),回调);
mUiHandler =新的处理程序(回调);
mHtHandler.sendEmptyMessageDelayed(0,3000);
您当然可以摆脱回调的接口,并创建两个处理程序与覆盖的handleMessage方法...
I want to set up a HandlerThread from the GUI thread. Then some time later, when a button is clicked on the GUI, it runs callHello(), which then send a message to a HelloLogger object residing on the non-GUI thread which asynchronously logs "Hello World". I have tried a number of things, some block indefinitely, some never receive the message, etc etc. The code below is more or less as close as I have got, please could someone modify it to work?
public class HandlerThreadExample {
private MyHandlerThread mMyHandlerThread;
private Looper mLooper;
private Handler mHandler;
public HandlerThreadExample(){
mMyHandlerThread = new MyHandlerThread();
mMyHandlerThread.start();
mLooper = mMyHandlerThread.getLooper();
}
public void callHello() {
mHandler.sendEmptyMessage(1);
}
private class MyHandlerThread extends HandlerThread {
private HelloLogger mHelloLogger;
private Handler mHandler;
public MyHandlerThread() {
super("The MyHandlerThread thread", HandlerThread.NORM_PRIORITY);
}
public void run (){
mHelloLogger = new HelloLogger();
mHandler = new Handler(getLooper()){
public void handleMessage(Message msg){
mHelloLogger.logHello();
}
};
super.run();
}
}
private class HelloLogger {
public HelloLogger (){
}
public void logHello(){
Log.d("HandlerThreadExample", "Hello World");
}
}
}
Best examples found:
HandlerThread Test How to create a Looper thread, then send it a message immediately? Async calls with Handler HandlerThread vs Executor - When is one more appropriate over the other? Best use of HandlerThread over other similar classes Android HandlerThread HandlerThread examples Android: Passing data between main and worker threads Java Synchronised Sending messages between threads using activity thread queue and Handler class Intro to Loopers and Handlers developer.android: Specifying the Code to Run on a ThreadAt least now I can close the damned tabs
Solution courtesy of help from pskink
public class HandlerThreadExample2 {
private static int MSG_START_HELLO = 0;
private static int MSG_HELLO_COMPLETE = 1;
private HandlerThread ht;
private Handler mHtHandler;
private Handler mUiHandler;
private boolean helloReady = false;
public HandlerThreadExample2(){
ht = new HandlerThread("The new thread");
ht.start();
Log.d(App.TAG, "UI: handler thread started");
mUiHandler = new Handler(){
public void handleMessage(Message msg){
if (msg.what == MSG_HELLO_COMPLETE){
Log.d(App.TAG, "UI Thread: received notification of sleep completed ");
helloReady = true; }
}
};
mHtHandler = new Handler(ht.getLooper()){
public void handleMessage (Message msg){
if (msg.what == MSG_START_HELLO){
Log.d(App.TAG, "handleMessage " + msg.what + " in " + Thread.currentThread() + " now sleeping");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(App.TAG, "Woke up, notifying UI thread...");
mUiHandler.sendEmptyMessage(MSG_HELLO_COMPLETE);
}
}
};
}
public void sendLongHello(){
if (helloReady){
Log.d(App.TAG, "sending hello " + Thread.currentThread());
mHtHandler.sendEmptyMessage(MSG_START_HELLO);
helloReady = false;
} else {
Log.e(App.TAG, "Cannot do hello yet - not ready");
}
}
}
解决方案
so this is the working example:
HandlerThread ht = new HandlerThread("MySuperAwesomeHandlerThread");
ht.start();
Handler h = new Handler(ht.getLooper()) {
public void handleMessage(Message msg) {
Log.d(TAG, "handleMessage " + msg.what + " in " + Thread.currentThread());
};
};
for (int i = 0; i < 5; i++) {
Log.d(TAG, "sending " + i + " in " + Thread.currentThread());
h.sendEmptyMessageDelayed(i, 3000 + i * 1000);
}
UPDATE:
make two class fields:
Handler mHtHandler;
Handler mUiHandler;
and try this:
HandlerThread ht = new HandlerThread("MySuperAwsomeHandlerThread");
ht.start();
Callback callback = new Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == 0) {
Log.d(TAG, "got a meaasage in " + Thread.currentThread() + ", now sleeping... ");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(TAG, "woke up, notifying ui thread...");
mUiHandler.sendEmptyMessage(1);
} else
if (msg.what == 1) {
Log.d(TAG, "got a notification in " + Thread.currentThread());
}
return false;
}
};
mHtHandler = new Handler(ht.getLooper(), callback);
mUiHandler = new Handler(callback);
mHtHandler.sendEmptyMessageDelayed(0, 3000);
you can of course get rid of Callback interface and create two Handlers with overridden handleMessage method...
相关推荐
最新文章