-1

我是 java 新手,我想知道我的手机是否在 10 秒内丢失了 wi-fi 连接以发送带有他的 GPS 信息的短信(对于一个应用程序,该应用程序使用 pc 请求的位置和传感器数据制作 http 页面)。我只是不知道如何进行,是否有 OnStateChange 之类的?我必须如何实现该代码: http: //developer.android.com/reference/android/net/NetworkInfo.DetailedState.html这样做?

感谢回复。

4

2 回答 2

1

尝试这个

public static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager connMgr = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected() ||
        connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()){

        Log.d("conected","the server is connected");
        Toast.makeText(ctx, "Network is available", Toast.LENGTH_LONG).show();
            return true;
    }
    Log.d("conected","the server is not connected");
    Toast.makeText(ctx, "Network is not available", Toast.LENGTH_LONG).show();

    return false;
 } 

}

于 2013-10-25T13:00:43.627 回答
1

这边走:

public static boolean isInternetAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}
于 2013-10-25T12:48:55.057 回答