Android - Programmatically check internet connection and display dialog if notConnected

后端 未结 14 2484
予麋鹿
予麋鹿 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 04:44

    This cup of code helps you to check internet connection available or not from the API level 16 to 30

    @SuppressWarnings("deprecation")
    public static boolean isInternetAvailable(Activity activity) {
        ConnectivityManager conMgr = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Network network = conMgr.getActiveNetwork();
            NetworkCapabilities networkCapabilities = conMgr.getNetworkCapabilities(network);
            if (networkCapabilities != null) {
                return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
            } else return false;
        } else {
            // below API Level 23
            return conMgr.getActiveNetworkInfo() != null
                    && conMgr.getActiveNetworkInfo().isAvailable()
                    && conMgr.getActiveNetworkInfo().isConnected();
        }
    }
    
    0 讨论(0)
  • 2020-12-15 04:45

    I've tried different methods described above, besides it I've been caught in some cases. So finally, I'm using below code snippet to make a network call...

    try {
        InetAddress address = InetAddress.getByName("www.stackoverflow.com");
        //Connected to working internet connection
    } catch (UnknownHostException e) {
        e.printStackTrace();
        //Internet not available
    }
    

    This can be useful in many perspectives. If something is not fine in this approach, please let me know.

    0 讨论(0)
  • 2020-12-15 04:52
    if (InternetConnection.checkConnection(context)) {
        // Internet Available...
    } else {
        // Internet Not Available...
    }
    

    just copy below class

    public class InternetConnection {
    
        /** CHECK WHETHER INTERNET CONNECTION IS AVAILABLE OR NOT */
        public static boolean checkConnection(Context context) {
            final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
            NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
    
            if (activeNetworkInfo != null) { // connected to the internet
                Toast.makeText(context, activeNetworkInfo.getTypeName(), Toast.LENGTH_SHORT).show();
    
                if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    // connected to wifi
                    return true;
                } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                    // connected to the mobile provider's data plan
                    return true;
                }
            }
            return false;
        }
    }
    

    Give following permission to manifest

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
    0 讨论(0)
  • 2020-12-15 04:53

    Internet connection also check either in onCreate() or onResume() method. There is no need to call inside the AsyncTask.

    or You can call before call the AsyncTask execute() method

    if(isOnline)
    {
       MyAsyncTask task=new MyAMyAsyncTask();
       task.execute();
    
    }
    
    else
    {
    // no internet connection
    }
    
    0 讨论(0)
  • 2020-12-15 04:55

    Check internet connection

     ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfo netInfo = mgr.getActiveNetworkInfo();
    
            if (netInfo != null) {
                if (netInfo.isConnected()) {
                    // Internet Available
                }else {
                   //No internet
                }
            } else {
                //No internet
            }
    
    0 讨论(0)
  • 2020-12-15 04:56

    doInBackground runs on a different Thread than the main UI, so you can't create a show a dialog here. Instead, override onPreExecute in your AsyncTask, and do the test there.

    0 讨论(0)
提交回复
热议问题