英文:
warning: [deprecation] TYPE_MOBILE in ConnectivityManager has been deprecated?
问题
以下是您提供的代码的中文翻译部分:
package com.aniketjain.weatherapp.network;
import android.content.Context;
import android.net.ConnectivityManager;
public class InternetConnectivity {
public static boolean isInternetConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean connection_flag = false;
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == android.net.NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == android.net.NetworkInfo.State.CONNECTED) {
connection_flag = true;
}
return connection_flag;
}
}
警告:[已弃用] ConnectivityManager 中的 TYPE_MOBILE 已被弃用。
英文:
package com.aniketjain.weatherapp.network;
import android.content.Context;
import android.net.ConnectivityManager;
public class InternetConnectivity {
public static boolean isInternetConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean connection_flag = false;
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == android.net.NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == android.net.NetworkInfo.State.CONNECTED) {
connection_flag = true;
}
return connection_flag;
}
}
warning: [deprecation] TYPE_MOBILE in ConnectivityManager has been deprecated
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == android.net.NetworkInfo.State.CONNECTED ||
答案1
得分: 1
以下是翻译好的代码部分:
这是一个示例用于:
val isNetworkAvailable = (context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).run {
getNetworkCapabilities(activeNetwork)?.run {
hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
|| hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|| hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
} ?: false
}
如果您打算在较旧的版本中使用`TYPE_MOBILE`,您可以在上面添加API级别检查:
`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)`
英文:
here is an example to use for:
val isNetworkAvailable = (context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).run {
getNetworkCapabilities(activeNetwork)?.run {
hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
|| hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|| hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
} ?: false
}
If you aim to use TYPE_MOBILE
for older version, you can add check on above for API level:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
答案2
得分: 0
Yes.
This has been deprecated in API level 28.
你可以看到有一个建议,你应该使用以下方法代替:
> 应用程序应该使用 NetworkCapabilities#hasTransport 或
> requestNetwork(android.net.NetworkRequest,
> android.net.ConnectivityManager.NetworkCallback) 来请求适当的网络。请参阅 NetworkCapabilities 以获取支持的传输方式。
英文:
Yes.
This has been deprecated in API level 28.
You can see that there is a suggestion on what you should use instead:
> Applications should instead use NetworkCapabilities#hasTransport or
> requestNetwork(android.net.NetworkRequest,
> android.net.ConnectivityManager.NetworkCallback) to request an
> appropriate network. See NetworkCapabilities for supported transports.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论