gpt4 book ai didi

java - 检查 Gprs 连接

转载 作者:行者123 更新时间:2023-11-29 03:58:23 35 4
gpt4 key购买 nike

我想通过代码显示在android中检查gprs连接是否活跃我该如何检查。我有以下代码。会成功吗?

public static boolean isOnline(Context context) {
try {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
URL url = new URL("https://telemeter4tools.services.telenet.be/TelemeterService");
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setRequestProperty("User-Agent", "My Android Demo");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000); // mTimeout is in seconds

urlc.connect();

if (urlc.getResponseCode() == 200) {
return true;
} else {
return false;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

我可以用谷歌替换那个网站吗并检查响应。

最佳答案

Check below code:

public boolean checkNetworkStatus() {

final ConnectivityManager connMgr = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);

final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (wifi.isAvailable()) {
Toast.makeText(this, "Wifi connection.", Toast.LENGTH_LONG).show();
return true;

} else if (mobile.isAvailable()) {
Toast.makeText(this, "GPRS connection.", Toast.LENGTH_LONG).show();
return true;

} else {
Toast.makeText(this, "No network connection.", Toast.LENGTH_LONG).show();
return false;
}

}

关于java - 检查 Gprs 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5027890/

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