2013-08-19 128 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

这不是他所寻求的 –