[警告] ConnectivityManager 中的 TYPE_MOBILE 已被弃用。

huangapple go评论68阅读模式
英文:

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.

[警告] ConnectivityManager 中的 TYPE_MOBILE 已被弃用。

参考

你可以看到有一个建议,你应该使用以下方法代替:

> 应用程序应该使用 NetworkCapabilities#hasTransport 或
> requestNetwork(android.net.NetworkRequest,
> android.net.ConnectivityManager.NetworkCallback) 来请求适当的网络。请参阅 NetworkCapabilities 以获取支持的传输方式。

英文:

Yes.
This has been deprecated in API level 28.

[警告] ConnectivityManager 中的 TYPE_MOBILE 已被弃用。

Reference

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.

huangapple
  • 本文由 发表于 2023年7月6日 20:30:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定