I want to check whether the internet connectivity is available or not when the application is started, and it should display a message in both the cases- 1.if internet connection is there it should ask user 'would you like to proceed further or not' 2.and when it is not connected then it should say 'internet connection is not available'
6 Answers
You're looking for the ConnectivityManager. It will allow you to query the state of the network hardware on the device. If you need further guidance, let me know!

- 6,532
- 1
- 46
- 57
-
Looks like the newer answers have posted some sample code for you. – Jon O Apr 01 '12 at 18:10
This is probably similar to this:
How to check internet connectivity in android?
Perform it either on the intial Activity or on the app's onCreate() method.
public static boolean haveInternet(Context ctx) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to
// disable internet while roaming, just return false
return false;
}
return true;
}

- 11,416
- 3
- 26
- 38
You definitely want to use a BroadcastReceiver, filtering CONNECTIVITY_CHANGED actions. When you rcv CONNECTIVITY_CHANGED, you can then use NetworkInfo to check the status of the connection (you can even see if you are connected via mobile or wifi).
Registering a BoardcastReceiver to a service or activity in your app all will allow your app to recognize when your connection goes down or changes while it is running so it is much better than just checking it once when the app starts (which it also does).
private BroadcastReceiver mConnectivityChanged = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (info.isConnected()) {
// ....
} else {
// ....
}
}
}
public void onCreate() {
this.registerReceiver(mConnectivityChanged,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
public void onDestroy() {
this.unregisterReceiver(mConnectivityChanged);
}
Typed this by hand so sorry if there are typos :).

- 1,336
- 14
- 25
For simplicity and efficient reusabality,make a seperate class Connection and then do the same as below (Internet connection can be either through sim or wifi,so check whichever you need)
import android.content.Context;
import android.net.ConnectivityManager;
public class Connection {
public static boolean CheckInternet(Context context)
{
System.out.println("in checkinternet()");
ConnectivityManager connec = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// Here if condition check for wifi and mobile network is available or not.
// If anyone of them is available or connected then it will return true, otherwise false;
if ((wifi!= null)&&(wifi.isConnected())) {
return true;
} else if (!mobile.isConnected()) {
return false;
} else if ((mobile!= null)&&(mobile.isConnected())) {
return true;
}
return false;
}
}
and then in your launch acitivty's onCreate method,
Boolean connection_check= Connection.CheckInternet(getApplicationContext());
if(connection_check==true)
{
//Ask the question "Would you like to proceed" using a dialog box or anyway else
}
else{
System.out.println(connection_check);
Toast.makeText(getApplicationContext(),"Internet Connection Not Available", Toast.LENGTH_SHORT).show();
}

- 361
- 1
- 11
//To check whether network connection is available on device or not
public static boolean checkInternetConnection(Activity _activity) {
ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
return true;
else
return false;
}//checkInternetConnection()

- 5,357
- 4
- 16
- 17