So many people think that how to find internet connection status in Android.
In AndroidSDk Provide ConnectivityManager Class for all queries about
the state of network connectivity.
and also notifies applications when network connectivity changes.
This Example i will explain how to detect internet connection status in your applications.
To find status of network state the below permissions are mandatory in your AndroidManifest file
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
UI Layout(check_internet_connection.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"
tools:context=".CheckInternetConnection" >
<TextView
android:id="@+id/aboutapp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="This Example Expains Check internet connection status"
android:textSize="15sp" />
<Button
android:id="@+id/checkstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="44dp"
android:text="Click me i will tell you" />
</RelativeLayout>
Android Activity(CheckInternetConnection.java)
In Activity isconnection() method check internet connection status and return true if your mobile have a internet connection else return false.
To find status of internet connection
we use ConnectivityManager in isconnection() method
package com.androidsurya.checkinternetconnection;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class CheckInternetConnection extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_internet_connection);
Button check = (Button) findViewById(R.id.checkstatus);
check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (isConnecting()) {
Toast.makeText(getApplicationContext(),
"Your Mobile have internet connection",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Your Mobile don't have internet connection",
Toast.LENGTH_LONG).show();
}
}
});
}
public boolean isConnecting() {
ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.check_internet_connection, menu);
return true;
}
}
Register Activity in AndroidManifest file
<activity
android:name="com.androidsurya.checkinternetconnection.CheckInternetConnection"
android:label="@string/app_name" >
Output Screenshot:
For more information about ConnectivityManager: Android Developers