Wednesday, 13 July 2011

Checking Internet Connection

Step 1: I had checked Alert Message , when the Internet is Disconnected.

  So created a method, which checks the Internet Connection

public boolean netCheck()
{
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
//notify user you are not online
bs_netcheck =true;

}
return bs_netcheck;
}



Step 2: Create a Method which Shows Alert Dialog as i shown below

public void displayAlert()
{
new AlertDialog.Builder(this).setMessage("Please Check Your Internet Connection and Try Again")
.setTitle("Network Error")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
}



Step 3: Now Call this method  as i shown below
Boolean  bs_netcheck=false;
bs_netcheck = netCheck();
if(bs_netcheck == true)
{
displayAlert();
}

No comments:

Post a Comment