Friday, February 22, 2013

How to detect Internet connection after launching Android app

Some android apps may require users to have an active internet connection and may crash without one. The following implementation will allow detection of Internet connection. If an active connection does not exist, the user will be notified.

1. Create a new JAVA class named InternetCheck.java and paste the following code

package com.samples.myapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;


public class InternetCheck extends Activity {
 // flag for Internet connection status
 Boolean isInternetPresent = false;

 // Connection detector class
 ConnectionDetector cd;
 

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  // creating connection detector class instance
  cd = new ConnectionDetector(getApplicationContext());
 
  // get Internet status
  isInternetPresent = cd.isConnectingToInternet();

  // check for Internet status
  if (isInternetPresent) {
   // Internet Connection is Present
   // make HTTP requests
   showAlertDialog(InternetCheck.this, "Internet Connection",
     "You have internet connection", true);
  } else {
   // Internet connection is not present
   // Ask user to connect to Internet
   showAlertDialog(InternetCheck.this, "No Internet Connection",
     "You don't have internet connection.", false);
  }

 }
 /**
  * Function to display simple Alert Dialog
  * @param context - application context
  * @param title - alert dialog title
  * @param message - alert message
  * @param status - success/failure (used to set icon)
  * */
 public void showAlertDialog(Context context, String title, String message, Boolean status) {
  AlertDialog alertDialog = new AlertDialog.Builder(context).create();

  // Setting Dialog Title
  alertDialog.setTitle(title);

  // Setting Dialog Message
  alertDialog.setMessage(message);
 
  // Setting alert dialog icon
  alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

  // Setting OK Button
  alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
   }
  });

  // Showing Alert Message
  alertDialog.show();
 }
}

No comments:

Post a Comment