Showing posts with label network. Show all posts
Showing posts with label network. Show all posts

Tuesday, October 11, 2016

Code Snippet SD Cards Network check

Code Snippet SD Cards Network check


In many occassions, we may find a need to check for online condition and download files to our sd card.

PFB the code to check for network availability,

public boolean isNetworkAvailable() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;

}

PFB the code for SD Card mount check,

private boolean isSDCardMounted() {
String sdCardStatus = android.os.Environment.getExternalStorageState();
return sdCardStatus.equals(android.os.Environment.MEDIA_MOUNTED);
}

PFB the code that returns available space in SD Card,

public int getFreeExtMemory()
{
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
    int free  =  ((statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1024);
    Log.i("AppInstallChecker", "Available Space : " + free + " KB, Required Space : "+APPSIZE_KB+" KB");
    return free;
}

Note : The methods getAvailableBlocks() & getBlockSize() from the above method/snippet is deprecated after api 18, so put a check if needed.

References :
http://stackoverflow.com/questions/5474089/how-to-check-currently-internet-connection-is-available-or-not-in-android
http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts
http://stackoverflow.com/questions/7429228/check-whether-the-sd-card-is-available-or-not-programatically
http://stackoverflow.com/questions/3394765/how-to-check-available-space-on-android-device-on-mini-sd-card


Available link for download

Read more »