Network connection check in Android always returns false altough network is available

2.3k Views Asked by At

I would like to check in my App (before writing to an online database) if I have a network connection. For this purpose I use the code from this accepted answer on Stackoverflow check network connection

public boolean isNetworkAvailable() {
    Log.e("LogTag", "Cecking connection");
    Runtime runtime = Runtime.getRuntime();
    try {
        Process process = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int exitValue = process.waitFor();
        return (exitValue == 0);
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
    return false;
}

However, I always get the following error

2021-11-13 12:13:38.812 17259-17259/com.example.td.bapp E/LogTag: Cecking connection
2021-11-13 12:13:40.837 359-359/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2021-11-13 12:13:40.837 359-359/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2021-11-13 12:13:47.021 372-372/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument

2021-11-13 12:13:47.021 372-372/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe

In another Stackoverflow question Failed to open QEMU pipe is was said that the problem is due to the Android Emulator and it was recommended to use android:usesCleartextTraffic="true" in the Android manifest, which I did. But the error still occurs and the method isNetworkAvailable always returns false altough I have a Internet connection (I tried this 50 times).

Here is the code of my Android manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.td.bapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />

    </application>

</manifest>

Do you know what the problem might be and how to solve this issue?

Update: I also tried another solution that I found here Android check internet connection

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com");
        return !ipAddr.equals("");

    } catch (Exception e) {
        return false;
    }
}

But this also always yields false altough I have Internet connection.

0

There are 0 best solutions below