how to determine, if an internet connection is currently available and active on an android device?

孤人 提交于 2019-12-03 20:26:10
Octavian Damiean

Take a look at this answer here on StackOverflow. It should be just what you are looking for.

boolean connected;            
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
                    connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
                //we are connected to the internet
                connected = true;
            }
            else
                connected = false;

add persmission: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Just try to connect to your server, and timeout if you haven't started download progress in an acceptable period of time.

virusss8

Here is the best answer to problem:

Device is connected on router but there is no internet gateway active.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!