gpt4 book ai didi

java - 如何使用 Connectivity manager.NetworkCallback 获取网络类型

转载 作者:行者123 更新时间:2023-12-02 09:07:25 25 4
gpt4 key购买 nike

在我的项目中,我想显示所使用的连接类型。例如,当用户启用 wifi 并打开应用程序时如果使用移动数据,则会显示启用 WiFi 的 toast,然后应显示另一个 toast。

我使用以下代码来显示类型,但我发现 TYPE_WIFITYPE_MOBILE 已弃用,建议使用 ConnectivityManager.NetworkCallback();。所以我很困惑如何使用它

请帮助我知道该怎么做

这是我的代码 fragment

public void CheckConnection(){

ConnectivityManager manager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = manager.getActiveNetworkInfo();

if(null!=activeNetwork){

if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI){

Toast.makeText(this, "Connected with Wi-Fi", Toast.LENGTH_SHORT).show();
}
else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE){

Toast.makeText(this, "Connected with Cellular Data", Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(this,"No Internet connection Detected",Toast.LENGTH_SHORT).show();
}

}

最佳答案

试试这个:

public class NetworkUtil {

public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;


public static int getConnectivityStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
return TYPE_WIFI;

if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
return TYPE_MOBILE;
}
return TYPE_NOT_CONNECTED;
}

public static String getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
String status = null;
if (conn == NetworkUtil.TYPE_WIFI) {
status = "Wifi enabled";
} else if (conn == NetworkUtil.TYPE_MOBILE) {
status = "Mobile data enabled";
} else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
status = "Not connected to Internet";
}
return status;
}
}

第二个创建广播接收器,您将在其中获得所有这些更改:

public class NetworkChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, final Intent intent) {

String status = NetworkUtil.getConnectivityStatusString(context);

Toast.makeText(context, status, Toast.LENGTH_LONG).show();
}
}

并且不要忘记将您的广播添加到 mainfest 中:

  <receiver
android:name="YOUR PACKAGE NAME"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>

也是 getType() 的替代方法:在 kotlin 中

    private fun isInternetAvailable(context: Context): Boolean {
var result = false
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val actNw =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
result = when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
} else {
connectivityManager.run {
connectivityManager.activeNetworkInfo?.run {
result = when (type) {
ConnectivityManager.TYPE_WIFI -> true
ConnectivityManager.TYPE_MOBILE -> true
ConnectivityManager.TYPE_ETHERNET -> true
else -> false
}

}
}
}

return result
}

java 中 getType() 的替代方法:

 public static boolean isInternetAvailable(Context context) { 
boolean result = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (cm != null) {
NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork());
if (capabilities != null) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
result = true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
result = true;
}
}
}
} else {
if (cm != null) {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
// connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
result = true;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
result = true;
}
}
}
}
return result;
}

关于java - 如何使用 Connectivity manager.NetworkCallback 获取网络类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59704596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com