2014-02-16 35 views
0

我正在尝试创建一个新的Android应用程序,在该应用程序中我必须识别其范围内的任何新的和现有的WiFi网络。在Android中实现服务以识别WiFi网络

所以我需要一个始终在后端运行的服务。我检查了Service和IntentService类,我认为这些类可以实现这一点。但我不确定要使用哪一个服务或Intentservice?

回答

1

为了检查网络连接的可用性,您可以使用下面的代码:

public boolean isNetworkAvailable() { 
    ConnectivityManager myconnectivity = (ConnectivityManager) context 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 
    if (myconnectivity == null) { 
     return false; 
    } else { 
     NetworkInfo[] myinfo = myconnectivity.getAllNetworkInfo(); 
     if (myinfo != null) { 
      for (int i = 0; i < myinfo.length; i++) { 
       if (myinfo[i].isConnected()) { 
        return true; 
       } 
      } 
     } 
    } 
    return false; 
} 

您可以检查MyInfo的[I]实例来检查,如果这个网络类型为WiFi或不是。请参阅此文档:http://developer.android.com/reference/android/net/NetworkInfo.html

+0

感谢您的回复。但我使用wifimanager类来检测wifi。 – ChanChow