什么是Android P API 28中不推荐使用的ConnectivityManager.TYPE_WIFI的替代方法? [英] What is alternative to ConnectivityManager.TYPE_WIFI deprecated in Android P API 28?

查看:648
本文介绍了什么是Android P API 28中不推荐使用的ConnectivityManager.TYPE_WIFI的替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ConnectivityManager.TYPE_WIFI 。 , NetworkInfo#getType ConnectivityManager.TYPE_MOBILE 也已弃用。

那么,有什么替代方案呢?我了解到,我们必须使用 NetworkCapabilities 类中的方法。但是,我无法在一个地方合并所有内容,例如如何在NetworkCapabilities类中进行getType()以及如何在其上添加WIFI和蜂窝数据检查?

So, what are the alternatives for them? I understood that the we've to use the method from NetworkCapabilities class. However I'm not able to merge all the things in one place like how to do getType() in NetworkCapabilities class and how to add the WIFI and cellular data checks on it?

请协助。

推荐答案

ConnectivityManager.TYPE_WIFI 已弃用 。您应该使用 NetworkCapabilities

这将替换旧的 ConnectivityManager。TYPE_MOBILE 网络选择方法。该应用程序应指定需要高带宽,而不是因为应用程序需要高带宽并在出现新的快速网络(如LTE)时有过时的风险而表明需要Wi-Fi,而是应指定它需要高带宽。同样,如果应用程序需要不计量的网络进行批量传输,则可以指定而不是假设对所有基于蜂窝的连接进行计量而不对所有基于Wi-Fi的连接进行计量。

This replaces the old ConnectivityManager.TYPE_MOBILE method of network selection. Rather than indicate a need for Wi-Fi because an application needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE), the application should specify it needs high bandwidth. Similarly if an application needs an unmetered network for a bulk transfer it can specify that rather than assuming all cellular based connections are metered and all Wi-Fi based connections are not.


应用程序应改为使用 NetworkCapabilities.hasTransport(int)
requestNetwork(NetworkRequest,NetworkCallback) 来请求一个
合适的网络。

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

这篇关于什么是Android P API 28中不推荐使用的ConnectivityManager.TYPE_WIFI的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆