我得到了一个AsyncTask,应该检查对主机名的网络访问。但是doInBackground()永远不会超时。有人知道吗?
public class HostAvailabilityTask extends AsyncTask<String, Void, Boolean> {
private Main main;
public HostAvailabilityTask(Main main) {
this.main = main;
}
protected Boolean doInBackground(String... params) {
Main.Log("doInBackground() isHostAvailable():"+params[0]);
try {
return InetAddress.getByName(params[0]).isReachable(30);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean... result) {
Main.Log("onPostExecute()");
if(result[0] == false) {
main.setContentView(R.layout.splash);
return;
}
main.continueAfterHostCheck();
}
}
Android提供了ConnectivityManager类来了解互联网连接状态。下面的方法将是非常有用的,以了解互联网连接状态。
首先在AndroidManifest.xml中添加INTERNET和ACCESS_NETWORK_STATE权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
然后使用下面的方法检查设备是否连接到互联网。如果设备已连接到互联网,此方法将返回true。
public boolean isInternetAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
}
参考链接:—http://www.androidtutorialshub.com/android-check-internet-connection-status/
不需要太复杂。最简单的框架方式是使用ACCESS_NETWORK_STATE权限并创建一个连接的方法
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null &&
cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
如果您有特定的主机和连接类型(wifi/移动),也可以使用requestRouteToHost。
你还需要:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在你的android清单中。
我在这里看到了很多过时的答案,所以我决定加入我的答案。
由于Android 10 (API级别29)getActiveNetworkInfo()已弃用,谷歌建议我们使用NetworkCallbacks而不是针对Android 10及更高版本的应用程序。
关于阅读网络状态的文档提供了一些关于如何使用NetworkCallback的信息,但我没有设法找到一个很好的代码示例,整个事情的工作,所以这里是我提出的代码,我们在我们的应用程序中使用:
import android.content.Context
import android.net.ConnectivityManager
import android.net.LinkProperties
import android.net.Network
import android.net.NetworkCapabilities
import com.fieldontrack.kmm.common.network.ConnectivityMonitor
import com.fieldontrack.kmm.entities.connectivity.NetworkType
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
class ConnectivityMonitorImpl(appContext: Context) : ConnectivityMonitor {
private val connectivityManager = appContext.getSystemService(ConnectivityManager::class.java)
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) =
connectivityManager.getNetworkCapabilities(network)?.let { networkCapabilities ->
updateConnectionStatus(networkCapabilities = networkCapabilities)
updateNetworkType(networkCapabilities = networkCapabilities)
} ?: run {
_isConnectedState.value = true
}
override fun onLost(network: Network) {
// Do not check for NetworkCapabilities here, as they might be wrong.
// If we get this callback, we're certain that we've lost connection.
_isConnectedState.value = false
_networkTypeState.value = NetworkType.Unknown
}
override fun onCapabilitiesChanged(
network: Network,
networkCapabilities: NetworkCapabilities
) {
updateConnectionStatus(networkCapabilities = networkCapabilities)
updateNetworkType(networkCapabilities = networkCapabilities)
}
override fun onLinkPropertiesChanged(
network: Network,
linkProperties: LinkProperties
) = Unit
}
private val _isConnectedState = MutableStateFlow(false)
private val _networkTypeState = MutableStateFlow(NetworkType.Unknown)
override val isConnectedState: StateFlow<Boolean> = _isConnectedState
override val networkTypeState: StateFlow<NetworkType> = _networkTypeState
override val isConnected: Boolean
get() = _isConnectedState.value
override val networkType: NetworkType
get() = _networkTypeState.value
init {
startMonitoring()
}
override fun startMonitoring() =
connectivityManager.registerDefaultNetworkCallback(networkCallback)
override fun stopMonitoring() =
connectivityManager.unregisterNetworkCallback(networkCallback)
private fun updateConnectionStatus(networkCapabilities: NetworkCapabilities) {
val isConnected =
networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
_isConnectedState.value = isConnected
}
private fun updateNetworkType(networkCapabilities: NetworkCapabilities) {
val networkType = when {
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> NetworkType.WiFi
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> NetworkType.Cellular
else -> NetworkType.Unknown
}
_networkTypeState.value = networkType
}
}
ConnectivityMonitor界面非常简单:
interface ConnectivityMonitor {
val isConnected: Boolean
val networkType: NetworkType
val isConnectedState: StateFlow<Boolean>
val networkTypeState: StateFlow<NetworkType>
fun startMonitoring()
fun stopMonitoring()
}
NetworkType只是一个简单的枚举:
enum class NetworkType { Unknown, Cellular, WiFi }
据我测试,无论应用程序是在后台还是前台,这都是可行的。