0

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.

VanessaF
  • 515
  • 11
  • 36
  • Are you positive that the internet is available? Emulator is sometimes really fragile, for example connecting to the vpn can require emulator restart to get the internet – Jakoss Nov 14 '21 at 09:41
  • @Jakoss: Thanks for your comment. Yes, I am 100 % sure that a Internet connection is available also for the Emulator. I can verify that by storing the data on an Internet database (Firebase database). Even if the Internet checking methods posted above return false, I try to store the data on Firebase Database and I can see in realtime, that the data is indeed stored there. Further I get a positive feedback from LogTag that the data could be submitted (because the write method to Firebase Database was successfull). – VanessaF Nov 14 '21 at 09:50
  • And in the second method you shown - what's the exception thrown there? – Jakoss Nov 14 '21 at 13:15
  • Thanks for the comment Jakoss. I get a similar error message as posted above: 2021-11-14 16:25:33.388 283-283/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument 2021-11-14 16:25:33.389 283-283/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe 2021-11-14 16:25:33.870 278-278/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument 2021-11-14 16:25:33.870 278-278/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument – VanessaF Nov 14 '21 at 15:28
  • @Jakoss: Any comments on my last comment? I'll appreciate it. – VanessaF Nov 15 '21 at 18:44
  • Not really, it seems like some qemu/emulator issue. You should probably report that directly to google – Jakoss Nov 15 '21 at 22:02
  • @Jakoss: Thanks for your answer and effort. I don't think that google would care about my request. But thanks for your hints. – VanessaF Nov 16 '21 at 20:07
  • It's quite possible to be an emulator issue. Try it on a real device. – Alex Mamo Nov 17 '21 at 07:51
  • 1
    @AlexMamo: Thanks for your comment. I tested the code on another Emulator but the result was the same. Those methods always return false altough I have an Internet connection. I will try to look for another solution for checking the Internet connection. – VanessaF Nov 17 '21 at 18:49

0 Answers0