ZooZoo ZooZoo - 2 months ago 20
Android Question

Display an alert when internet connection not available in android application

In my application data comes from internet and I am trying to create a function that checks if a internet connection is available or not and if it isn't, it gives an alert messege that no internet connection available.
i am using following code. but its not working.

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
if (isOnline())
{
// my code
}
else
{
Hotgames4meActivity1.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
try {
AlertDialog alertDialog = new AlertDialog.Builder(Hotgames4meActivity1.this).create();

alertDialog.setTitle("Info");
alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again");
//alertDialog.setIcon(R.drawable.alerticon);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();

}
});

alertDialog.show();
}
catch(Exception e)
{
//Log.d(Constants.TAG, "Show Dialog: "+e.getMessage());
}
}


}

Answer
public void onCreate(Bundle obj)
{
    super.onCreate(obj)
    setContextView(layout);

    if (isOnline())
        //do whatever you want to do 
    else
        {
        try {
    AlertDialog alertDialog = new AlertDialog.Builder(con).create();

    alertDialog.setTitle("Info");
    alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again");
    alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
         finish();

       }
    });

    alertDialog.show();
    }
    catch(Exception e)
    {
        Log.d(Constants.TAG, "Show Dialog: "+e.getMessage());
    }
        }
}