Friday, March 11, 2011

Checking Internet Connection and Displaying Alert Dialog Box in Android

Hi Friends, Let us know, how to display Alert Dialog in Android

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

Notification in Android Using AlarmManager, BoradCastReceiver & Services

    Our aim is to show notification message to user in a specific time.     Say For example , I have planned to show Notification ...