Android P API 28 中弃用的 ConnectivityManager.TYPE_WIFI 的替代方法是什么?
What is alternative to ConnectivityManager.TYPE_WIFI deprecated in Android P API 28?
ConnectivityManager.TYPE_WIFI is deprecated in Android P API 28. Also, NetworkInfo#getType and ConnectivityManager.TYPE_MOBILE 也已弃用。
那么,他们有哪些选择呢?我了解到我们必须使用 NetworkCapabilities class 中的方法。但是,我无法将所有内容合并到一个地方,例如如何在 NetworkCapabilities class 中执行 getType() 以及如何在其上添加 WIFI 和蜂窝数据检查?
请协助。
ConnectivityManager.TYPE_WIFI
已 弃用 。你应该使用 NetworkCapabilities
.
这取代了旧的 ConnectivityManager.TYPE_MOBILE
网络选择方法。与其指出需要 Wi-Fi,因为应用程序需要高带宽并且在出现新的快速网络(如 LTE)时有过时的风险,应用程序应该指定它需要高带宽。同样,如果应用程序需要不按流量计费的网络进行批量传输,它可以指定而不是假设所有基于蜂窝的连接都按流量计费,而所有基于 Wi-Fi 的连接都不是。
Applications should instead use NetworkCapabilities.hasTransport(int)
or requestNetwork(NetworkRequest, NetworkCallback)
to request an
appropriate network. for supported transports.
你可以这样试试
NetworkAgentInfo networkAgent;
int type = ConnectivityManager.TYPE_NONE;
if (networkAgent.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
type = ConnectivityManager.TYPE_MOBILE;
} else if (networkAgent.networkCapabilities.hasTransport(
NetworkCapabilities.TRANSPORT_WIFI)) {
type = ConnectivityManager.TYPE_WIFI;
}
您可以使用以下代码段来检查您是否有 Wifi 连接或移动网络:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Network network = connectivityManager.getActiveNetwork();
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR));
}
使用以下方法.. 19/06/2019
public boolean isconnectedToWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Network network = connectivityManager.getActiveNetwork();
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
if (capabilities == null) {
return false;
}
return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
} else {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo == null) {
return false;
}
return networkInfo.isConnected();
}
}
这就是我如何在不使用已弃用的库和添加对 marshamallow 设备的支持的情况下检查 WIFI 是否可用的方法-
@ExperimentalCoroutinesApi
suspend fun isWifiAvailable(): Boolean {
val connectivityManager =
ContextCompat.getSystemService(context, ConnectivityManager::class.java) ?: return false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val activeNetwork =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
return activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
} else {
return connectivityManager.isNetworkAvailable(NetworkCapabilities.TRANSPORT_WIFI)
// val activeNetwork = connectivityManager.`activeNetworkInfo`
// return activeNetwork?.type == ConnectivityManager.TYPE_WIFI
}
}
@ExperimentalCoroutinesApi
private suspend fun ConnectivityManager.isNetworkAvailable(vararg transportType: Int): Boolean {
return suspendCancellableCoroutine { continuation ->
val builder = NetworkRequest.Builder()
transportType.forEach {
builder.addCapability(it)
}
val networkRequest = builder.build()
val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
continuation.resume(true, null)
unregisterNetworkCallback(this)
}
override fun onLost(network: Network) {
super.onLost(network)
continuation.resume(false, null)
unregisterNetworkCallback(this)
}
override fun onUnavailable() {
super.onUnavailable()
continuation.resume(false, null)
unregisterNetworkCallback(this)
}
}
registerNetworkCallback(networkRequest, networkCallback)
}
}
ConnectivityManager.TYPE_WIFI is deprecated in Android P API 28. Also, NetworkInfo#getType and ConnectivityManager.TYPE_MOBILE 也已弃用。
那么,他们有哪些选择呢?我了解到我们必须使用 NetworkCapabilities class 中的方法。但是,我无法将所有内容合并到一个地方,例如如何在 NetworkCapabilities class 中执行 getType() 以及如何在其上添加 WIFI 和蜂窝数据检查?
请协助。
ConnectivityManager.TYPE_WIFI
已 弃用 。你应该使用 NetworkCapabilities
.
这取代了旧的 ConnectivityManager.TYPE_MOBILE
网络选择方法。与其指出需要 Wi-Fi,因为应用程序需要高带宽并且在出现新的快速网络(如 LTE)时有过时的风险,应用程序应该指定它需要高带宽。同样,如果应用程序需要不按流量计费的网络进行批量传输,它可以指定而不是假设所有基于蜂窝的连接都按流量计费,而所有基于 Wi-Fi 的连接都不是。
Applications should instead use
NetworkCapabilities.hasTransport(int)
orrequestNetwork(NetworkRequest, NetworkCallback)
to request an appropriate network. for supported transports.
你可以这样试试
NetworkAgentInfo networkAgent;
int type = ConnectivityManager.TYPE_NONE;
if (networkAgent.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
type = ConnectivityManager.TYPE_MOBILE;
} else if (networkAgent.networkCapabilities.hasTransport(
NetworkCapabilities.TRANSPORT_WIFI)) {
type = ConnectivityManager.TYPE_WIFI;
}
您可以使用以下代码段来检查您是否有 Wifi 连接或移动网络:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Network network = connectivityManager.getActiveNetwork();
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR));
}
使用以下方法.. 19/06/2019
public boolean isconnectedToWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Network network = connectivityManager.getActiveNetwork();
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
if (capabilities == null) {
return false;
}
return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
} else {
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo == null) {
return false;
}
return networkInfo.isConnected();
}
}
这就是我如何在不使用已弃用的库和添加对 marshamallow 设备的支持的情况下检查 WIFI 是否可用的方法-
@ExperimentalCoroutinesApi
suspend fun isWifiAvailable(): Boolean {
val connectivityManager =
ContextCompat.getSystemService(context, ConnectivityManager::class.java) ?: return false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val activeNetwork =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
return activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
} else {
return connectivityManager.isNetworkAvailable(NetworkCapabilities.TRANSPORT_WIFI)
// val activeNetwork = connectivityManager.`activeNetworkInfo`
// return activeNetwork?.type == ConnectivityManager.TYPE_WIFI
}
}
@ExperimentalCoroutinesApi
private suspend fun ConnectivityManager.isNetworkAvailable(vararg transportType: Int): Boolean {
return suspendCancellableCoroutine { continuation ->
val builder = NetworkRequest.Builder()
transportType.forEach {
builder.addCapability(it)
}
val networkRequest = builder.build()
val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
continuation.resume(true, null)
unregisterNetworkCallback(this)
}
override fun onLost(network: Network) {
super.onLost(network)
continuation.resume(false, null)
unregisterNetworkCallback(this)
}
override fun onUnavailable() {
super.onUnavailable()
continuation.resume(false, null)
unregisterNetworkCallback(this)
}
}
registerNetworkCallback(networkRequest, networkCallback)
}
}