任务是检查手机是否连接到互联网。我有问题。即使wifi关闭,它也会显示“已连接”。这是我的课。
public class InterneProvjera {
Context context;
@SuppressLint("MissingPermission")
public InterneProvjera(Context context){
this.context = context;
}
public boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (NetworkInfo i: info) {
if (i.getState() == NetworkInfo.State.CONNECTED)
return true;
}
}
}
return false;
}
}
这是主要活动:
InterneProvjera interneProvjera = new InterneProvjera(this);
String tKonekcija = (interneProvjera.isNetworkAvailable()) ? "Connected" : "No connection";
txtIspis.setText(tKonekcija);
抱歉,如果它在 android 编程中是新的,那么它的微不足道的问题。Ps:是否有任何连接监听器以及如何检查互联网信号强度(3G、4G、wifi)?