I want to create a method which tells if the device is online. As far as I understand ConnectivityManager tells only if the device is connected to a network. This doesn't mean that the device is connected to the internet. To ensure that the device is online I'm using InetAddress.getByName("google.com").isReachable(3);
but I can't use it on the main thread. I can create a separate thread to check the connectivity and then use a callback function but is there another way? I don't want my app to do anything before it is connected. Do you have any solutions? Thank you!
我想创建一个方法,告诉设备是否在线。据我所知,ConnectivityManager仅告知设备是否连接到网络。这并不意味着设备已连接到互联网。为了确保设备在线,我正在使用InetAddress.getByName(“google.com”)。isReachable(3);但我不能在主线程上使用它。我可以创建一个单独的线程来检查连接,然后使用回调函数,但还有另一种方法吗?我不希望我的应用程序在连接之前做任何事情。你有什么解决办法?谢谢!
2 个解决方案
#1
With any networking, there isn't a guaranteed way to check whether or not you are connected to an endpoint without actually sending data. Even if the device is connected to a network, has an ip address, recently received data, e.t.c, it doesn't mean that you still have a connection.
对于任何网络,都没有保证在没有实际发送数据的情况下检查您是否连接到端点的方法。即使设备连接到网络,有一个IP地址,最近收到的数据,e.t.c,也不意味着你仍然有连接。
I would suggest allowing the user to progress into your application as much as possible, queuing up the requests to your server in the background whilst a connection is established. Use the same framework to resend data if the connection is lost whilst the user is using the app. Make the connection to the server as transparent to the user as possible, unless it fails to connect after ~1 minute
我建议允许用户尽可能地进入您的应用程序,在建立连接的同时在后台将请求排队到您的服务器。如果在用户使用应用程序时连接丢失,请使用相同的框架重新发送数据。使与服务器的连接尽可能对用户透明,除非在约1分钟后无法连接
#2
try this:
public static boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
#1
With any networking, there isn't a guaranteed way to check whether or not you are connected to an endpoint without actually sending data. Even if the device is connected to a network, has an ip address, recently received data, e.t.c, it doesn't mean that you still have a connection.
对于任何网络,都没有保证在没有实际发送数据的情况下检查您是否连接到端点的方法。即使设备连接到网络,有一个IP地址,最近收到的数据,e.t.c,也不意味着你仍然有连接。
I would suggest allowing the user to progress into your application as much as possible, queuing up the requests to your server in the background whilst a connection is established. Use the same framework to resend data if the connection is lost whilst the user is using the app. Make the connection to the server as transparent to the user as possible, unless it fails to connect after ~1 minute
我建议允许用户尽可能地进入您的应用程序,在建立连接的同时在后台将请求排队到您的服务器。如果在用户使用应用程序时连接丢失,请使用相同的框架重新发送数据。使与服务器的连接尽可能对用户透明,除非在约1分钟后无法连接
#2
try this:
public static boolean isOnline(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}