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 方法中的 toast 出现了,这意味着收到了消息,但通知没有到达线程,这意味着它处于等待状态。

4

1 回答 1

3

您正在使用两个不同的对象进行同步!一个是线程对象,一个是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();
}
于 2012-09-10T20:26:55.600 回答