2016-10-02 3 views
0

나는 와이파이 연결을 기다리는 목적으로 처리기를 사용하려고합니다. 당신이 코드 조각에서 볼 수 있듯이while 루프에서 handler를 사용하고 이벤트를 기다리는 방법?

final AlertDialog alertDialog2 = new AlertDialog.Builder(new android.view.ContextThemeWrapper(context, R.style.AlertDialogCustom)).create(); 
    alertDialog2.setTitle("Loading..."); 
    alertDialog2.setIcon(R.drawable.check); 
    alertDialog2.show(); 
    Handler handler = new Handler(); 
    int count = 0; 
    while (!isConnected() /*Check wifi connection*/) { 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       alertDialog2.dismiss(); 
       // do other thing 
      } 
     }, 200); 
     count++; 
     /*stop the loop after 20s*/ 
     if (count > 100) { 
      break; 
     } 
    } 

, 내가 운전 중 로딩에 AlertDialog를 보여주고 싶은과가 완료 때 나는 통지를 중지하고 싶습니다 :이 코드 조각은 내가 사용하고 있습니다 사용자와 Wi-Fi 연결.

답변

0

WIFI 광범위한 캐스트 수신기를 사용해야합니다.

먼저 대화 상자를 표시 한 다음 WIFI 상태가 변경 될 때 알려주고 대화 상자를 닫으려는 상태를 수신하면 알려주는 wifi 광범위한 전송 수신기를 등록해야합니다.

는 WIFI 상태를 감지하는 방법을 알기 위해 아래 링크를 참조하면 답변

How to detect when WIFI Connection has been established in Android?

final AlertDialog alertDialog2 = new AlertDialog.Builder(new android.view.ContextThemeWrapper(context, R.style.AlertDialogCustom)).create(); 
alertDialog2.setTitle("Loading..."); 
alertDialog2.setIcon(R.drawable.check); 
alertDialog2.show(); 

private final BroadcastReceiver myReceiver = new BroadcastReceiver() { 

@Override 
    public void onReceive(Context context, Intent intent) { 
     if(intent.getAction().equalsIgnoreCase("android.net.wifi.STATE_CHANGE")) { 
      Log.d(TAG,"WIFI STATE CHANGED"); 
alertDialog2. dismiss(); 
     } 
    } 
}; 

IntentFilter intent = new IntentFilter(""); 
registerReceiver(myReceiver, intent); 
+0

감사를 변경하지만 문제는 내가 와이파이가 연결되어있는 경우에만 통지 것입니다. 그러나 그것이 실패했는지 어떻게 알 수 있습니까? 그게 내가 타이머가 필요한 이유입니다. –