Search code examples
androidandroid-contentprovider

Android ContentProvider check network access


is there any way how to take advantage of ContentProvider to check, whether my Android phone is connected to the internet?

Thanks


Solution

  • // check internet connectivity
    public static boolean isNetworkAvailable(Context context) {
    
        boolean networkStatus;
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
    
            networkStatus = (activeNetworkInfo != null && activeNetworkInfo.isConnected()) ? true : false;
    
        } catch (Exception e) {
            e.printStackTrace();
            DinotaLogger.log(e, Level.SEVERE);
            networkStatus = false;
        }
    
        return networkStatus;
    }
    

    Please make sure to put in manifest file. If you are using an emulator to check this, press F8 to disable network access. This works fine with android 2.3.3 emulator.