2012-09-10 28 views
0

通知線程這是我的主線程:從接收機

Thread t = new Thread(){ 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     for(int i = 0; i < passes.length; i++){ 
      progPass.setProgress(i); 
      WifiConfiguration wc = new WifiConfiguration(); 
      wc.SSID = "\"" + dbw.getSsid() + "\""; 
      wc.preSharedKey = "\"" + passes[i] + "\""; 
      wc.status = WifiConfiguration.Status.ENABLED; 
      wc = configureCapabs(wc, dbw.getCapability()); 
      int res = wifi.addNetwork(wc); 
      //Toast.makeText(this, "add Network returned " + res , Toast.LENGTH_SHORT).show(); 
      boolean b = wifi.enableNetwork(res, true);   
      //Toast.makeText(this, "enableNetwork returned " + b , Toast.LENGTH_SHORT).show(); 
      if(!b) continue; 
      try { 
       synchronized(this){ 
        this.wait(); 
       } 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      boolean fin = wifi.getConnectionInfo() != null; 
      if(fin) break; 
     } 
     progPass.dismiss(); 
     this.interrupt(); 
    } 

}; 

它檢查的wifi使用比較常見的密碼之一。當它等待來自廣播接收通知的等待線,這裏是它的代碼:

public class ConnectivityActionReceiver extends BroadcastReceiver{ 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { 
     NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); 
     if(networkInfo.isConnected()) { 
      // Wifi is connected 
      Toast.makeText(context, "Wifi is connected: " + String.valueOf(networkInfo), Toast.LENGTH_SHORT).show(); 
      synchronized(this){ 
       notifyAll(); 
      } 
     } 
    } else if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { 
     NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 
     if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI && ! networkInfo.isConnected()) { 
      // Wifi is disconnected 
      Toast.makeText(context, "Wifi is disconnected: " + String.valueOf(networkInfo), Toast.LENGTH_SHORT).show(); 
     } 
    } else if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)){ 
     int esr = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0); 
     if(esr != 0){ 
      Toast.makeText(context, Integer.toString(esr), Toast.LENGTH_LONG).show(); 
      synchronized(this){ 
       notifyAll(); 
      } 
     } 

    } 
} 

這是註冊清單:

<receiver 
     android:name="ConnectivityActionReceiver" 
     android:enabled="true" 
     android:label="ConnectivityActionReceiver" > 
     <intent-filter> 
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
      <action android:name="android.net.wifi.STATE_CHANGE" /> 
      <action android:name="android.net.wifi.supplicant.STATE_CHANGE" /> 
     </intent-filter> 
    </receiver> 

它在同一類的實例作爲線程:

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    IntentFilter filter = new IntentFilter(); 
    this.registerReceiver(car, filter); 
} 

應該出現在的onReceive方法的吐司顯示出來,這意味着消息被接收,但通知沒有到達線程,這意味着它停留在等待。

回答

1

您正在使用兩個不同的對象進行同步!一個是線程對象,一個是ConnectivityActionReceiver。當你使用this

synchronized(this){ 
     this.wait(); 
} 

你鎖定在當前的實例,因此可以有效地防止在你的情況不同的實例。

的解決方法是調用wait/notify一個共同的,全局可見對象上:

final Object signal = new Object(); 

// in the waiting thread 
synchronized(signal) { 
    signal.wait(); 
} 

// in the signaling thread 
synchronized(signal) { 
    signal.notifyAll(); 
}