开发者

Check network in Android

How do I check any kind of network 开发者_高级运维status in Android?


if (Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
        ConnectivityManager.TYPE_MOBILE)
    || Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
        ConnectivityManager.TYPE_WIFI) {

    // do something
}


For checking the network this function will help you.

`public boolean InternetConnection() 

{
        ConnectivityManager connectivity = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }`

if the network is there it will return true otherwise it will return false.


can check network availability or not

ConnectivityManager connectivityManager 
    = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜