异步问题

时间:2017-10-04 13:10:53

标签: java android asynchronous broadcastreceiver wifimanager

我遇到了这个代码的问题它接收它正在运行异步并且它返回false但为时已晚,因为if语句中的代码在onReceve中完成检查之前运行。

如何首先检查onReceve,然后在if语句中运行代码?

class WifiReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context c, Intent intent) {
        String action  = intent.getAction();
        if(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)){
            Log.d("WifiReceiver", ">>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<");
            int supl_error=intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
            if(supl_error==WifiManager.ERROR_AUTHENTICATING){
                Log.i("ERROR_AUTHENTICATING", "ERROR_AUTHENTICATING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                flag = false;
            }
            else {
                flag = true;
            }
        }
    }
}

if(flag){
//do something
}

1 个答案:

答案 0 :(得分:1)

将Async完成后要调用的代码放入方法并在onReceive()中调用方法。