androidkotlinconnectionconnectivity

How to check internet connection in android without ping or using deprecated code?


I see many answer for this question but most of the answers using deprecated code & many using ping method to check active internet connection on android that results of getting wrong result on some devices like Samsung. I need a code that could be small & efficient.


Solution

  • Try this one out its simple and efficient

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager)youractivity.this.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
                    if (info == null)
                    {
                        Toast.makeText(login.this,"No Internet Connection",Toast.LENGTH_SHORT).show();
                    }