我正在嘗試對一些代碼做一份書面報告,我在 youtube 上找到了一個。但是我不明白這個回圈是如何作業的。我知道它必須回傳一個布林值,然后進入另一個方法,但如果有人可以分解正在發生的事情,那將不勝感激。
public class Loop {
public static boolean isConnectedToInternet(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i )
if (info[i].getState() == NetworkInfo.State.CONNECTED)
return true;
}
return false;
}
}
uj5u.com熱心網友回復:
我添加了一些評論以供理解:
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(context.CONNECTIVITY_SERVICE);
//making the object
if (connectivityManager!=null){ //if it was instantiated
NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); //it makes an array of all of the network info
if (info!=null){ //if this worked (if there's info)
for (int i=0;i<info.length;i ){ //looping through data
if (info[i].getState() == NetworkInfo.State.CONNECTED)
//if the state of any of the objects is equal to connected (if device is connected)
return true; //the device is online
}
}
}
return false; //the device is not online
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/342442.html
