2013-08-19 170 views
1

嗨我在我的android應用程序的登錄屏幕中使用此代碼來檢查網絡連接是否可用。並且工作正常。繼續檢查網絡連接android

public static boolean checkNetworkConnection(Context context) { 
    final ConnectivityManager connMgr = (ConnectivityManager) context 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 

    final android.net.NetworkInfo wifi = connMgr 
      .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    final android.net.NetworkInfo mobile = connMgr 
      .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

    if (wifi.isConnectedOrConnecting() ||mobile.isConnectedOrConnecting()) 
     return true; 
    else 
     return false; 
} 

但是我想到了一個案例 -

我的應用從服務器獲取的一些數據和table.Now顯示我打開應用程序,並同時獲取數據連接出現of.Now我不要刷新應用程序或頁面以從服務器獲取數據。 我想要一個後臺進程應該運行或線程將繼續檢查連接性,一旦它獲得連接,它應該自動獲取數據。

任何機構都可以幫助我解決這個問題。

回答

3

您不應該定期(或連續)檢查連接性。您應該聽取連接更改,如here所述。整個頁面是一個很好的教程,介紹如何在使用互聯網時不浪費網速。
基本上,你需要註冊一個BroadcastReceiver爲<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>,並實現你想要有邏輯:

public class NetworkReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     ConnectivityManager conn = (ConnectivityManager) 
     context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = conn.getActiveNetworkInfo(); 

     // Checks the user prefs and the network connection. Based on the result, decides whether 
     // to refresh the display or keep the current display. 
     // If the userpref is Wi-Fi only, checks to see if the device has a Wi-Fi connection. 
     if (WIFI.equals(sPref) && networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { 
      // If device has its Wi-Fi connection, sets refreshDisplay 
      // to true. This causes the display to be refreshed when the user 
      // returns to the app. 
      refreshDisplay = true; 
      Toast.makeText(context, R.string.wifi_connected, Toast.LENGTH_SHORT).show(); 

     // If the setting is ANY network and there is a network connection 
     // (which by process of elimination would be mobile), sets refreshDisplay to true. 
     } else if (ANY.equals(sPref) && networkInfo != null) { 
      refreshDisplay = true; 

     // Otherwise, the app can't download content--either because there is no network 
     // connection (mobile or Wi-Fi), or because the pref setting is WIFI, and there 
     // is no Wi-Fi connection. 
     // Sets refreshDisplay to false. 
     } else { 
      refreshDisplay = false; 
      Toast.makeText(context, R.string.lost_connection, Toast.LENGTH_SHORT).show(); 
     } 
    } 
-1

使用此代碼..

public boolean CheckInternet() 
{ 
    ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

    // Here if condition check for wifi and mobile network is available or not. 
    // If anyone of them is available or connected then it will return true, otherwise false; 

    if (wifi.isConnected()) { 
     return true; 
    } else if (mobile.isConnected()) { 
     return true; 
    } 
    return false; 
} 
+2

這不是他所尋求的 –