0

I created a new project with an empty activity, and this is my full code:

MainActivity.java

package com.myapp.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import java.io.IOException;

public class MainActivity extends AppCompatActivity{

    public boolean isConnectedToInternet(){
        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;
    }

    @Override protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(isConnectedToInternet()){
            Toast.makeText(this, "connected to internet", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "not connected to internet", Toast.LENGTH_SHORT).show();
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.myapplication">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

When I start my application in the emulator, it shows a toast "not connected to internet" - why?

And the internet works on the emulator, I can use chrome in the emulator on the virtual device and it shows youtube.com etc correctly. - It only doesn't work in the application.

What am I missing?

  • Take a look here https://stackoverflow.com/a/9049884/7132300 – algrid Feb 11 '18 at 19:18
  • It could be because the Runtime in emulators is limited. You should check for internet connection in a different way, maybe try this out: [https://stackoverflow.com/a/4239019/2385906] – Marino Feb 11 '18 at 19:24

2 Answers2

0

Add

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

in your manifest outside of the application tag

Alexander Deych
  • 446
  • 2
  • 6
  • I added it above the application tag on the empty line, and there is no change. And I understand that this permission is no longer needed. There's anything else I can be missing? –  Feb 11 '18 at 19:08
  • @safebookverified Can you check your app on the real phone? https://groups.google.com/forum/#!topic/android-developers/hldQa-aBkpc it's possible that there may be ICMP limitations on the emulator – Alexander Deych Feb 11 '18 at 19:13
0

use this code to check intenet connection..in main

TestInternet testInternet = new TestInternet();
        testInternet.execute();

out of main

class TestInternet extends AsyncTask<Void, Void, Boolean>
        {
            @Override
            protected Boolean doInBackground(Void... params) {
                try {
                    URL url = new URL("http://www.google.com");
                    HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                    urlc.setConnectTimeout(4000);
                    urlc.connect();
                    if (urlc.getResponseCode() == 200) {
                        return true;
                    }
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                    return false;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return false;
                }
                return false;
            }

            @Override
            protected void onPostExecute(Boolean result)
            {
                if (!result)
                {
                    // code if not connected

                else
                    {
                        // code if connected
                    }
            }
        }