2014-02-23 126 views
0

我使用检查互联网连接下面的方法,但它崩溃..检查网络连接不工作

public static boolean CheckInternetConnection() { 

    ConnectivityManager connec = (ConnectivityManager) PropertyApplication 
      .getContext().getSystemService(Context.CONNECTIVITY_SERVICE); 
    android.net.NetworkInfo wifi = connec 
      .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    android.net.NetworkInfo mobile = connec 
      .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

    if (wifi != null && wifi.isConnected()) { 
     return true; 
    } else if (mobile != null && mobile.isConnected()) { 
     return true; 
    } 

    return false; 
} 

此外,我已经添加使用许可清单文件。这里是我的清单

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

,这里是我的logcat

02-23 09:43:37.549: D/dalvikvm(1896): GC_FOR_ALLOC freed 36K, 8% free 2867K/3108K, paused 7ms, total 8ms 
02-23 09:43:37.573: I/dalvikvm-heap(1896): Grow heap (frag case) to 3.909MB for 960012-byte allocation 
02-23 09:43:37.673: D/dalvikvm(1896): GC_FOR_ALLOC freed 2K, 7% free 3802K/4048K, paused 49ms, total 49ms 
02-23 09:43:37.729: D/main(1896): inside main 
02-23 09:43:37.837: D/libEGL(1896): loaded /system/lib/egl/libEGL_genymotion.so 
02-23 09:43:37.849: D/(1896): HostConnection::get() New Host Connection established 0xb93084a8, tid 1896 
02-23 09:43:37.877: D/libEGL(1896): loaded /system/lib/egl/libGLESv1_CM_genymotion.so 
02-23 09:43:37.901: D/libEGL(1896): loaded /system/lib/egl/libGLESv2_genymotion.so 
02-23 09:43:37.997: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented 
02-23 09:43:37.997: E/OpenGLRenderer(1896): Getting MAX_TEXTURE_SIZE from GradienCache 
02-23 09:43:38.017: E/OpenGLRenderer(1896): Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 
02-23 09:43:38.017: D/OpenGLRenderer(1896): Enabling debug mode 0 
02-23 09:43:39.745: D/main(1896): inside finally 
02-23 09:43:40.045: D/dalvikvm(1896): GC_FOR_ALLOC freed 248K, 11% free 3750K/4200K, paused 8ms, total 8ms 
02-23 09:43:40.069: I/dalvikvm-heap(1896): Grow heap (frag case) to 4.931MB for 1127532-byte allocation 
02-23 09:43:40.097: D/dalvikvm(1896): GC_FOR_ALLOC freed 1K, 9% free 4850K/5304K, paused 23ms, total 23ms 
02-23 09:43:40.257: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented 
02-23 09:43:42.513: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented 
02-23 09:43:49.373: D/dalvikvm(1896): GC_FOR_ALLOC freed 243K, 8% free 5201K/5644K, paused 54ms, total 65ms 
02-23 09:43:49.577: I/dalvikvm-heap(1896): Grow heap (frag case) to 6.237MB for 1011060-byte allocation 
02-23 09:43:49.653: D/dalvikvm(1896): GC_FOR_ALLOC freed 987K, 22% free 5200K/6632K, paused 71ms, total 71ms 
02-23 09:43:50.077: D/dalvikvm(1896): GC_FOR_ALLOC freed 11K, 22% free 5189K/6632K, paused 74ms, total 77ms 
02-23 09:43:50.709: I/dalvikvm-heap(1896): Grow heap (frag case) to 9.118MB for 4044204-byte allocation 
02-23 09:43:50.789: D/dalvikvm(1896): GC_FOR_ALLOC freed <1K, 14% free 9138K/10584K, paused 78ms, total 78ms 
02-23 09:43:51.089: D/main(1896): inside prop home onCreate 
02-23 09:43:51.129: I/Choreographer(1896): Skipped 148 frames! The application may be doing too much work on its main thread. 
02-23 09:43:51.285: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented 
02-23 09:43:53.609: D/main(1896): inside prop home intent 
02-23 09:43:53.989: D/main(1896): inside prop home starting intent 
02-23 09:43:54.101: D/main(1896): inside prop list onCreate 
02-23 09:43:54.101: D/main(1896): inside prop list try block 
02-23 09:43:54.109: D/main(1896): inside prop list try blocktrue 
02-23 09:43:54.117: W/System.err(1896): java.lang.NullPointerException 
02-23 09:43:54.121: W/System.err(1896):  at com.propvis.utils.PropertyUtil.CheckInternetConnection(PropertyUtil.java:61) 
02-23 09:43:54.137: W/System.err(1896):  at com.propvis.activity.PropertyListActivity.onCreate(PropertyListActivity.java:41) 
02-23 09:43:54.149: W/System.err(1896):  at android.app.Activity.performCreate(Activity.java:5133) 
02-23 09:43:54.149: W/System.err(1896):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
02-23 09:43:54.165: W/System.err(1896):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
02-23 09:43:54.165: W/System.err(1896):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
02-23 09:43:54.169: W/System.err(1896):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
02-23 09:43:54.185: W/System.err(1896):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
02-23 09:43:54.189: W/System.err(1896):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-23 09:43:54.201: W/System.err(1896):  at android.os.Looper.loop(Looper.java:137) 
02-23 09:43:54.205: W/System.err(1896):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
02-23 09:43:54.217: W/System.err(1896):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-23 09:43:54.217: W/System.err(1896):  at java.lang.reflect.Method.invoke(Method.java:525) 
02-23 09:43:54.241: W/System.err(1896):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
02-23 09:43:54.245: W/System.err(1896):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
02-23 09:43:54.253: W/System.err(1896):  at dalvik.system.NativeStart.main(Native Method) 
02-23 09:43:54.421: W/EGL_genymotion(1896): eglSurfaceAttrib not implemented 
+1

很抱歉,但,怎么能不返回一个值?它要么返回true,false,要么崩溃? – Merlevede

+0

我想这是崩溃.. –

+0

崩溃与什么异常? – EJP

回答

0

这是我使用的代码,我希望它为你的作品了。

public static boolean isNetworkAvailable(Context context) { 
    ConnectivityManager connectivityManager 
      = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
    return activeNetworkInfo != null; 
} 
0

另一种方法是简单地打开Socket对抗谷歌。当我需要测试连通性时,我在我的许多项目中使用了以下代码,它绝对有效。

Socket socket; 
final String host = "www.google.com"; 
final int port = 80; 
final int timeout = 30000; // 30 seconds 

try { 
    socket = new Socket(); 
    socket.connect(new InetSocketAddress(host, port), timeout); 
} 
catch (UnknownHostException uhe) { 
    Log.e("Sock", "I couldn't resolve the host you've provided!"); 
} 
catch (SocketTimeoutException ste) { 
    Log.e("Sock", "After a reasonable amount of time, I'm not able to connect, probably I don't have connectivity!"); 
} 
catch (IOException ioe) { 
    Log.e("Sock", "Hmmm... Sudden disconnection, probably you should retry!"); 
} 

虽然这可能会很棘手。正确地在UnknownHostException上,超时可能需要更长的时间,大约需要45秒来超时 - 但另一方面,这通常发生在无法解析主机时,因此这更像是意味着您的Internet访问配置了DNS配置错误这是不可能的),或者你的连接关闭(这很可能)。

无论如何,如果你想要对冲你的赌注,你可以通过两种方式解决这个问题:

  • 不要使用一台主机,使用IP地址来代替。您可能会在主机上多次使用ping多个Google的IP。例如:

    [email protected] ~/services $ ping www.google.com 
    PING www.google.com (173.194.40.179) 56(84) bytes of data. 
    
  • 另一个解决方法将启动一个线程WatchDog并完成所需要的时间后,连接尝试。显然,强制结束意味着没有成功,所以在你的情况下,Google会倒闭。

0

为了给你的样品在一个工作版本:

public static boolean checkInternetConnection(Context Context) { 

    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; 
}