Android - Programmatically check internet connection and display dialog if notConnected

后端 未结 14 2488
予麋鹿
予麋鹿 2020-12-15 04:04

I am working on a live project. and when user click on the app. the welcome screen appears(there is a webview on that screen). and if the internet is not connected then the

14条回答
  •  失恋的感觉
    2020-12-15 05:08

    Finally, I got the answer.

    ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
    if (netInfo == null){
        Description.setVisibility(View.INVISIBLE);
        new AlertDialog.Builder(WelcomePage.this)
            .setTitle(getResources().getString(R.string.app_name))
            .setMessage(getResources().getString(R.string.internet_error))
            .setPositiveButton("OK", null).show();
    }else{
        dialog = ProgressDialog.show(WelcomePage.this, "", "Loading...", true,false);
        new Welcome_Page().execute();
    }
    

提交回复
热议问题