Display an alert when internet connection not available in android application

后端 未结 13 718
有刺的猬
有刺的猬 2020-12-08 11:48

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

13条回答
  •  粉色の甜心
    2020-12-08 12:27

    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());
            }
        }
    }
    

提交回复
热议问题