开发者

How to check internet connection is active/inactive while wifi is enabled in android

开发者 https://www.devze.com 2023-03-10 11:45 出处:网络
I am making an android application. In this applicati开发者_运维百科on i am receiving data from server. When Wi-Fi is enabled and internet connection is active in our device then it returns the number

I am making an android application. In this applicati开发者_运维百科on i am receiving data from server. When Wi-Fi is enabled and internet connection is active in our device then it returns the number of record from the server but whenever wifi is enabled but inernet connection is not active or inactive then it gives the force close. I have already checked the wifi connection is enabled.

Please tell me this how can i check that internet is active in our device.

Thanks for advance


Try this code

 private boolean isNetworkAvailable() 
    {
        ConnectivityManager connectivityManager 
              = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消