Thursday 9 August 2012

Net check (Android)

Function for android developer to check internet connection with alert dialog


public boolean netCheck()
    {
        if (isOnline())
            return true;
        else
        {
           
            try {
                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                alertDialog.setTitle("Internet not available");
                alertDialog.setMessage("Do you want to on internet");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                        startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS),0);
                    }

                });
                alertDialog.setButton2("NO", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                        dialog.cancel();
                    }

                });
                alertDialog.show();
        }
        catch(Exception e)
        {
                //
        }
           }
        return false;
    }   
    public boolean isOnline() {
        ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
        if(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable())
              return false;
        return true;
}

No comments:

Post a Comment