Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 225
Abdul Waheed Avatar asked Nov 20 '25 19:11

Abdul Waheed


1 Answers

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();
                }
like image 193
MoonLight Avatar answered Nov 23 '25 08:11

MoonLight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!