Saturday, 17 August 2013

The main thread can't receive the message

The main thread can't receive the message

I start a thread to search WIFI process, if find some ScanResults ,use the
handler to send message to main thread.but in deed, main thread can't get
the message.i don't know why. pls help me TKS!
this is the souce code below: i define a handler to process search result
here:
public Handler handler = new Handler() {
public void handleMessage(Message msg) {
Log.i("Light", msg.what+"");
switch (msg.what) {
//Time out
case m_nWTSearchTimeOut:
Log.i("Light", "time out MSG");
...
break;
case m_nWTScanResult://get the scanresult
Log.i("Light", "process");
...
break;
case m_nWTConnectResult:// connected
...
break;
}
}
};
and this is the search process.send message to main thread.
public void beginDiscovery() {
Thread searchThread = new Thread(new Runnable() {
private long startTime = System.currentTimeMillis();
@Override
public void run() {
while (true) {
Log.i("Light", "begin");
if (null == mainHandler) {
mainHandler = new
Handler(mContext.getMainLooper());
}
// time out
if (System.currentTimeMillis() -
this.startTime >= TIME_OUT) {
Log.i("Light",
System.currentTimeMillis()+"");
Log.i("Light",
this.startTime+"");
Log.i("Light", "time out");
Message msg = mainHandler
.obtainMessage(WifiActivity.m_nWTSearchTimeOut);
mainHandler.sendMessage(msg);
Log.i("Light", "send
message");
Log.i("Light", msg.what+"");
break;
}
if (!isWifiConnect()) {
m_wiFiAdmin.openWifi();
}
m_wiFiAdmin.startScan();
clearWifiList();
Log.i("Light", "seach over");
if
(m_wiFiAdmin.getWifiList().size()
!= 0) {
Message msg = mainHandler
.obtainMessage(WifiActivity.m_nWTScanResult);
mainHandler.sendMessage(msg);
Log.i("Light", "send
message");
break;
}
}
}
});
searchThread.start();
}
but main thread can't receive the message

No comments:

Post a Comment