How to Check Network Status from Mobile Application in Android

In this tutorial how to check network status, Wifi status and mobile network status from  an Android Application is shown.

To connect with network most android applications use HTTP for sending and receiving the data.

It has two basic clients HttpURLConnection and ApacheHttpClient.

For Gingerbread and higher version we use HttpURLConnection as a client.

For checking network connection we have 2 methods getActiveNetworkInfo() method and isConnected() method.

Code:

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”60dp”
android:onClick=”nwInfo”
android:text=”Click Here” />

<TextView
android:id=”@+id/tv1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/button1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”41dp” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity{

TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv1);
}

public void nwInfo(View v)
{
ConnectivityManager cm=((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo ni=cm.getActiveNetworkInfo();
boolean nw=ni.isConnected();
ni=cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifi=ni.isConnected();
ni=cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobile=ni.isConnected();
String result=”Network connected: “+nw+”\nWifi connected: “+isWifi+”\nMobile connected: “+isMobile;
tv.setText(result);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.demo”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />
<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”></uses-permission>

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.demo.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>

https://youtu.be/lFRI1xSaatY

Add a Comment

Your email address will not be published. Required fields are marked *