2015-08-21 43 views
0

我正在做一個有登錄活動的應用程序,當我進入這個活動時,我要求用戶啓用WIFI或移動數據,如果沒有連接到我的服務器。我在對話框中給出3個選項,WIFI,MOBILE DATA和CANCEL。有沒有辦法等待連接?回調?聽衆?

public void checkConnectionDialog(Context context){ 

     if(dialogConnection != null && dialogConnection.isShowing()) return; 

     dialogConnection = new Dialog(context); 
     dialogConnection.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialogConnection.setCanceledOnTouchOutside(false); 
     dialogConnection.setCancelable(false); 

     dialogConnection.setContentView(R.layout.dialog_connection); 

     Button bt_data = (Button) dialogConnection.findViewById(R.id.bt_data); 
     Button bt_wifi = (Button) dialogConnection.findViewById(R.id.bt_wifi); 
     Button bt_cancel = (Button) dialogConnection.findViewById(R.id.bt_cancel); 


     bt_data.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)); 
       dialogConnection.dismiss(); 
      } 
     }); 

     bt_wifi.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); 
       dialogConnection.dismiss(); 
      } 
     }); 

     bt_cancel.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialogConnection.dismiss(); 
       setResult(RESULT_CANCELED, null); 
       finish(); 
      } 
     }); 

     dialogConnection.show(); 
    } 

通過Intents,我將用戶發送到WIFI或MOBILE DATA設置。如果用戶啓用wifi或數據並返回(按下後退按鈕),則系統需要幾秒鐘才能連接到互聯網。

如果用戶在啓用互聯網後單擊回來,當他返回到應用程序時,它仍然沒有連接。

我想知道的是如何讓應用程序等待連接繼續。

APP(未連接) - >意圖(WiFi或已啓用數據) - > APP(等到連接顯示視圖)

感謝您的幫助。

+0

創建一個BroadcastReceiver,當您連接到互聯網時它將觸發一個事件:) – Kedarnath

回答

1

您可以註冊一個BroadcastReceiver,它監聽您活動中的網絡更改。事情是這樣的:

BroadcastReceiver connectionReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent != null && intent.getAction().equals(ConnectivityManager 
       .CONNECTIVITY_ACTION)) { 

      boolean isConnected = ConnectivityManagerCompat.getNetworkInfoFromBroadcast 
        ((ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE), 
          intent).isConnected(); 

      if (isConnected) { 
       onConnectionEstablished(); 
      } else { 
       onConnectionAbsent(); 
      } 
     } 
    } 
}; 

連接建立和缺席只是空洞的方法這確實是你想要的東西,如:

public void onConnectionEstablished() { 
     // do something 
} 

public void onConnectionAbsent() { 
     // do something 
} 

註冊和註銷接收器和onResume

@Override 
protected void onResume() { 
    super.onResume(); 
    IntentFilter filter = new IntentFilter(); 
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 
    registerReceiver(connectionReceiver, filter); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    try { 
     unregisterReceiver(connectionReceiver); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } 
} 
0

您可以使用以下清單意圖接收連接更改。把它們作爲意圖過濾器放在reciver內。請注意,這只是從頭開始,我現在不能測試它!

<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
     <action android:name="android.net.wifi.STATE_CHANGE"/> 

您此接收每次連接發生了變化的時候,但你必須做你的廣播接收器的一些工作,以檢查它是否連接與否:

例如,你可以在的onReceive聽() :

@Override 
public void onReceive(Context context, final Intent intent) { 
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    if (info.isConnected()) { 
     //then You know it is connected and can do some stuff 
    }else{ 

     //not connected..... 
    } 
} 

例如,你現在可以開始服務做一些事情,告知您的用戶它是連接與否。

相關問題