Main navigation

Checking an Internet Connection in Android

adsense


Use following code in the AndroidManifest.xml :

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.androidtutorialpoint.checknetworkconnection" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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>

Please note that we have used the INTERNET and ACCESS_NETWORK_STATE permissions.

INTERNET permission is required by android application to connect to internet.
ACCESS_NETWORK_STATE required by ConnectivityMannager to use getActiveNetworkInfo()

Create a Layout File

We will use a button to get the network status. So we have used a LinearLayout
with a button in center.
Open activity_main.xml and paste following code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button android:id="@+id/checkInternetButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10dp" android:paddingLeft="10dp" android:text="@string/checkInternetConnection"/>
</LinearLayout>

Check for Internet in MainActivity.java

Open MainActivity.java and paste the following code.

activity_main.xml

 
public class MainActivity extends AppCompatActivity {
    private Button checkIntrnetButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkIntrnetButton = (Button)findViewById(R.id.checkInternetButton);
        checkIntrnetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                checkConnection();
            }
        });


    }

    protected boolean isOnline() {

        ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        } else {
            return false;
        }
    }

    public void checkConnection(){
        if(isOnline()){
            Toast.makeText(MainActivity.this, "You are connected to Internet", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(MainActivity.this, "You are not connected to Internet", Toast.LENGTH_SHORT).show();

        }

    }
}

Test your app by Turning off the Internet and Clicking on the Check Internet Connection!! button. You should get a message that you are not connected to internet as shown below.

not connected to Internet




Next, connect to internet by turning ON your Wifi or Mobile Data and Click on the Button again , You should get a message that your are connected to internet.

android connected to internet

Reader Interactions

Comments

Leave a Reply

Advertisment ad adsense adlogger