1

嗨,我在我的 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;
}

但我想到了一个案例——

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

任何机构都可以帮助我解决这个问题。

4

2 回答 2

3

您永远不应该定期(或持续)检查连接性。您应该监听连接更改,如此所述。整个页面是一个很好的教程,关于如何在使用互联网时不耗尽电池。
基本上,您需要为 注册一个 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();
        }
    }
于 2013-08-19T12:13:41.063 回答
-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;
}
于 2013-08-19T12:14:20.013 回答