Detect Internet Connection

Let's test a code in which you can detect if there is an active internet connectivity or not in your app programmatically.

Create one class named Functions

Create one method in that class as below:

public static boolean isConnecting(Context context) {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Now in your Activity, do this at where you want to check internet connection status:
if (Functions.isConnecting(this)) {
    // you have internet connection
    // do your stuff here
    
} else {
    Toast.makeText(this, "No internet connecion.", Toast.LENGTH_LONG).show();
}

Comments

  1. Harrah's Cherokee Casino - MapyRO
    Harrah's Cherokee Casino. 광양 출장마사지 Location: 10 경상남도 출장샵 miles west of 김포 출장샵 Asheville. Location: 통영 출장안마 12 miles from 전라북도 출장샵 Asheville. Address: 12.0 mile from Asheville. Map.

    ReplyDelete

Post a Comment

Popular posts from this blog

SQLiteDatabase (Without SqliteOpenHelper class)

Set current date to Button and Datepicker dialog

MVC Architecture in Android