继续检查网络连接android

时间:2013-08-19 12:09:43

标签: android android-service

您好我在我的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;
}

但我想到了一个案例 -

我的应用程序从服务器获取一些数据并显示在表中。现在我打开了应用程序,同时获取数据连接。现在我不想刷新应用程序或页面以从服务器获取数据。 我想要一个后台进程应该运行或线程,它将继续检查连接,一旦它获得连接,它应该自动获取数据。

任何人都能帮助我。

2 个答案:

答案 0 :(得分:3)

您不应该定期(或连续)检查连接。您应该按照here所述来监听连接更改。整个页面是一个很好的教程,说明如何在使用互联网时不要耗尽电池 基本上,您需要为<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>注册BroadcastReceiver,并在那里实现您想要的逻辑:

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 :(得分:-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;
}