我有我的主UI,我有一個AsyncTask運行。我想與AsyncTask溝通,讓它在非UI線程上運行一些東西。所以我想這樣做是:與AsyncTask線程通信?處理程序不工作?
protected class WifiMon extends AsyncTask<Context, Integer, String>
{
Context parent;
CoexiSyst coexisyst;
private static final String WIMON_TAG = "WiFiMonitor";
private int PCAP_HDR_SIZE = 16;
private int _scan_pkts_left;
public Handler _handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// We invoke a scan, and then read in all of the packets
// captured from the scan.
if(msg.obj == ThreadMessages.WIFI_SCAN_START) {
Log.d(TAG, "Got message to start Wifi scan");
int start_rxpkts = getRxPacketCount();
runCommand("/data/data/com.gnychis.coexisyst/files/iw dev wlan0 scan");
_scan_pkts_left = getRxPacketCount() - start_rxpkts;
Log.d(TAG, "Finished Wifi scan");
}
}
};
...
}
然而,似乎當傳入消息來如,的handleMessage()實際上是在UI線程中運行。我知道這是因爲runCommand()阻塞了5秒,而我的UI最終在5秒內沒有響應。
爲什麼的handleMessage()非UI線程上沒有運行?線程之間是否還有其他一些首選的通信方式?
忘記AyncTask並用針來代替。 )https://github.com/ZsoltSafrany/needle –