当前位置: 移动技术网 > IT编程>移动开发>Android > Android中判断网络是否连接实例详解

Android中判断网络是否连接实例详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

太上道德天尊,强生婴儿油,最美伪娘分类目录

android中判断网络是否连接实例详解

在android中,如何监测网络的状态呢,这个有的时候也是十分重要的,方法如下:

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(); //  

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网