Android中判断网络是否连接实例详解
Android中判断网络是否连接实例详解
在android中,如何监测网络的状态呢,这个有的时候也是十分重要的,方法如下:
publicclassConnectionDetector{
privateContext_context;
publicConnectionDetector(Contextcontext){
this._context=context;
}
publicbooleanisConnectingToInternet(){
ConnectivityManagerconnectivity=(ConnectivityManager)_context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivity!=null)
{
NetworkInfo[]info=connectivity.getAllNetworkInfo();
if(info!=null)
for(inti=0;i<info.length;i++)
if(info[i].getState()==NetworkInfo.State.CONNECTED)
{
returntrue;
}
}
returnfalse;
}
调用:
ConnectionDetectorcd=newConnectionDetector(getApplicationContext()); BooleanisInternetPresent=cd.isConnectingToInternet();//
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短