简体   繁体   中英

Can not check network info programmatically in battery save mode in android device

I want to check connection state in my android app. I call the next method in onResume :

public boolean isThereInternetConnection() {
        boolean isConnected = false;
        ConnectivityManager connectivityManager =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = (connectivityManager != null) ? connectivityManager.getActiveNetworkInfo() : null;
        if (networkInfo != null) {
            isConnected = networkInfo.isConnected();
        }
        return isConnected;
    }

In normal mode it works well. But when my device is on battery saver mode and I open my activity from background, method isThereInternetConnection() returns false sometimes (internet connection was well). For this I checked networkInfo.getDetailedState() and in battery saver mode it returns DetailedState.BLOCKED even I have good internet connection. So how to resolve this bug, how to check internet connection in battery saver mode and obtain real value true or false - connected to internet or not?

The best way to check internet is actually pinging to the internet. Sometimes the internet is connected but no network access.

public boolean isOnline() {
    try {
        int timeoutMs = 1500;
        Socket sock = new Socket();
        SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

        sock.connect(sockaddr, timeoutMs);
        sock.close();

        return true;
    } catch (IOException e) { return false; }
}

The above code will not run on the main thread and requires internet connectivity permission.

The code below is a bit slow but will work on the main thread. Requires internet connectivity permission:

public boolean isOnline() {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    }
    catch (IOException e)          { e.printStackTrace(); }
    catch (InterruptedException e) { e.printStackTrace(); }

    return false;
}

Ref: How to check internet access on Android

You can use this method :

public void checkInternetAndContinue() {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                int timeoutMs = 1500;
                Socket sock = new Socket();
                SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

                sock.connect(sockaddr, timeoutMs);
                sock.close();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        startWork(true);
                    }
                });
            } catch (IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        startWork(false);

                    }
                });
            }
        }
    });
    thread.start();
}

and continue work if there is internet

private void startWork(boolean b) {
     if(b){
         // do whatever you want when the internet is available    
     } else {
         // No Internet.
     }
}

Hope it help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM