0

I just have my android app but I can't get internet connection on it

this is my AndroidManifest

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <activity
        android:name="com.example.onscreenapp.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

so as you can see, i have the permissions on it.

and i tried to use 2 functions to check the internet status

one is this one:

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name

        if (ipAddr.equals("")) {
            return false;
        } else {
            return true;
        }

    } catch (Exception e) {
        return false;
    }

}

which returns false, and the another one is this one:

public final boolean isInternetOn() {

    // get Connectivity Manager object to check connection
    ConnectivityManager connec =
            (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);

    // Check for network connections
    if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
            connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
            connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
            connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {

        // if connected with internet

        Toast.makeText(this, " Connected ", Toast.LENGTH_LONG).show();
        return true;

    } else if (
            connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                    connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {

        Toast.makeText(this, " Not Connected ", Toast.LENGTH_LONG).show();
        return false;
    }
    return false;
}

I'm testing this app by device on an android S4 mini, and when I compile it and start it I get "appname has stopped" showing like a runtime error.

Kara
  • 6,115
  • 16
  • 50
  • 57

5 Answers5

1

put below line in menifest not in application

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Arpit Patel
  • 1,561
  • 13
  • 23
0
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.stackoverflowandroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.onscreenapp.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>


----------
    public static boolean isInternetAvailable(Context c) {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) c
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

Use Above manifest and Connectionavailable code

koutuk
  • 832
  • 1
  • 8
  • 17
0

The isInternetAvailable() method is returning false. So, this means that though you are connected to a network, you are unable to access the Internet because the following line of code :

InetAddress ipAddr = InetAddress.getByName("google.com");

is basically a ping to Google.

Please, post the logcat to expect better help.

P.S. - You may also refer to @Squonk's answer here : Detect whether there is an Internet connection available on Android

Community
  • 1
  • 1
vkm
  • 548
  • 7
  • 23
0

Sorry for late reply but was looking this up cause I had the same problem with my App on running it on a 4.4 android tablet and on my log cat it showed something was wrong with the code that queried the internet connection method. So i deleted the following lines where i have commented them out in double slash and now my app works. Maybe the case with this one where you have to do the same. Hope it works for you as it did for me. Below is the code…

    //A method that checks to see if the Users phone is connected to the internet or not
    public boolean isConnetedToInternetOrNot() {

          // get Connectivity Manager object to check connection

        connec =    (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);

        if (
             //connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED //||
            // connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING //||
           //  connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
             connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED 

             ) {

             return true;

        } else {

            return false;

        }
    }
Charlie
  • 170
  • 12
0

put this code in your AndroidManifest xml file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
KIBOU Hassan
  • 381
  • 4
  • 13