设为首页 加入收藏

TOP

怎样检查Android网络连接状态
2014-11-24 11:47:44 来源: 作者: 【 】 浏览:0
Tags:怎样 检查 Android 网络 连接 状态

< xml version="1.0" encoding="utf-8" >
package="com.example.detectinternetconnection"
android:versionCode="1"
android:versionName="1.0" >



android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:name=".AndroidDetectInternetConnectionActivity"
android:label="@string/app_name" >















package com.example.detectinternetconnection;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
this._context = context;
}

public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.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;
}
}


ConnectionDetector cd = new ConnectionDetector(getApplicationContext());

Boolean isInternetPresent = cd.isConnectingToInternet(); // true or false


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇配置EditPlus使其可以编译运行Jav.. 下一篇IEEE802.11数据帧在Linux上的抓取

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)
·Redis - The Real-ti (2025-12-26 08:20:50)
·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)