代码的基本逻辑是这样工作线程:class ThreadWorker implements Runnable
{
private final Object mLock = new Object();
private Looper mLooper;
private Handler mhandler;
private final String DEBUG = "threadRecord";
/**
* Creates a worker thread with the given name. The thread
* then runs a [email=%7B@link]{@link[/email] android.os.Looper}.
* @param name A name for the new thread
*/
ThreadWorker( String name)
{
Thread t = new Thread(null, this, name);
t.setPriority(Thread.MIN_PRIORITY);
t.start();
synchronized (mLock)
{
while (mLooper == null)
{
try
{
mLock.wait();
}
catch (InterruptedException ex)
{
}
}
}
}
public Looper getLooper()
{
return mLooper;
}
public void run()
{
synchronized (mLock)
{
Looper.prepare();
mLooper = Looper.myLooper();
mLock.notifyAll();
}
mhandler = new Handler( mLooper )
{
@Override
public void handleMessage( Message msg )
{
Log.e( DEBUG, (String)(msg.obj) );
switch ( msg.what )
{
case 1:
// Log.i( DEBUG, "receive msg from main thread" );
break; default:
break;
}
}
};
String msgObj1 = "111***btnStopRecord message from mainThread";
Message msg1 = m_handlerRecord.obtainMessage(1, 1, 1, msgObj1 );
m_handlerRecord.sendMessage( msg1 );
String msgObj2 = "222***btnStopRecord message from mainThread";
Message msg2 = mhandler.obtainMessage(1, 1, 1, msgObj2 );
mhandler.sendMessage( msg2 );
Looper.loop();
}
public void quit()
{
mLooper.quit();
}
}主线程在oncreate的时候启动工作线程:
tWorker = new ThreadWorker( "aaaa" );
m_looperThreadRecord = tWorker.getLooper();
m_handlerRecord = new Handler( m_looperThreadRecord );然后在主线程的一个按钮事件里面发送:
Message msg = new Message();
msg.what = 1;
String strobj3 = "33333******";
msg.obj = strobj3;
tWorker.mhandler.sendMessage( msg );
hmsg = new Handler( tWorker.mLooper );
String strobj4 = "444444********444";
msg.obj = strobj4;
hmsg.sendMessage(msg);
现在问题如下:
工作线程能收到 22222 和 33333 的消息, 111111 和 44444 的消息收不到.
为何? 即工作线程里面初始化的hander对象发消息可以收到,主线程使用工作线程的looper对象new出来的handler发消息,工作线程就收不到.
{
private final Object mLock = new Object();
private Looper mLooper;
private Handler mhandler;
private final String DEBUG = "threadRecord";
/**
* Creates a worker thread with the given name. The thread
* then runs a [email=%7B@link]{@link[/email] android.os.Looper}.
* @param name A name for the new thread
*/
ThreadWorker( String name)
{
Thread t = new Thread(null, this, name);
t.setPriority(Thread.MIN_PRIORITY);
t.start();
synchronized (mLock)
{
while (mLooper == null)
{
try
{
mLock.wait();
}
catch (InterruptedException ex)
{
}
}
}
}
public Looper getLooper()
{
return mLooper;
}
public void run()
{
synchronized (mLock)
{
Looper.prepare();
mLooper = Looper.myLooper();
mLock.notifyAll();
}
mhandler = new Handler( mLooper )
{
@Override
public void handleMessage( Message msg )
{
Log.e( DEBUG, (String)(msg.obj) );
switch ( msg.what )
{
case 1:
// Log.i( DEBUG, "receive msg from main thread" );
break; default:
break;
}
}
};
String msgObj1 = "111***btnStopRecord message from mainThread";
Message msg1 = m_handlerRecord.obtainMessage(1, 1, 1, msgObj1 );
m_handlerRecord.sendMessage( msg1 );
String msgObj2 = "222***btnStopRecord message from mainThread";
Message msg2 = mhandler.obtainMessage(1, 1, 1, msgObj2 );
mhandler.sendMessage( msg2 );
Looper.loop();
}
public void quit()
{
mLooper.quit();
}
}主线程在oncreate的时候启动工作线程:
tWorker = new ThreadWorker( "aaaa" );
m_looperThreadRecord = tWorker.getLooper();
m_handlerRecord = new Handler( m_looperThreadRecord );然后在主线程的一个按钮事件里面发送:
Message msg = new Message();
msg.what = 1;
String strobj3 = "33333******";
msg.obj = strobj3;
tWorker.mhandler.sendMessage( msg );
hmsg = new Handler( tWorker.mLooper );
String strobj4 = "444444********444";
msg.obj = strobj4;
hmsg.sendMessage(msg);
现在问题如下:
工作线程能收到 22222 和 33333 的消息, 111111 和 44444 的消息收不到.
为何? 即工作线程里面初始化的hander对象发消息可以收到,主线程使用工作线程的looper对象new出来的handler发消息,工作线程就收不到.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货