|
|
|
@ -21,6 +21,7 @@ import android.content.Context; |
|
|
|
|
import android.net.ConnectivityManager; |
|
|
|
|
import android.net.Network; |
|
|
|
|
import android.net.NetworkCapabilities; |
|
|
|
|
import android.net.NetworkInfo; |
|
|
|
|
import android.net.NetworkRequest; |
|
|
|
|
import android.os.Build; |
|
|
|
|
import android.os.Handler; |
|
|
|
@ -120,6 +121,7 @@ public class AriaConfig { |
|
|
|
|
* 注册网络监听,只有配置了检查网络{@link AppConfig#isNetCheck()}才会注册事件 |
|
|
|
|
*/ |
|
|
|
|
private void regNetCallBack(Context context) { |
|
|
|
|
isConnectedNet = isNetworkAvailable(); |
|
|
|
|
if (!getAConfig().isNetCheck()) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
@ -155,6 +157,31 @@ public class AriaConfig { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public boolean isNetworkAvailable() { |
|
|
|
|
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
|
|
|
|
|
ConnectivityManager connectivityManager = |
|
|
|
|
(ConnectivityManager) getAPP().getSystemService(Context.CONNECTIVITY_SERVICE); |
|
|
|
|
|
|
|
|
|
if (connectivityManager == null) { |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} else { |
|
|
|
|
// 获取NetworkInfo对象
|
|
|
|
|
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo(); |
|
|
|
|
|
|
|
|
|
if (networkInfo != null && networkInfo.length > 0) { |
|
|
|
|
for (NetworkInfo info : networkInfo) { |
|
|
|
|
// 判断当前网络状态是否为连接状态
|
|
|
|
|
if (info.getState() == NetworkInfo.State.CONNECTED) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isConnectedNet() { |
|
|
|
|
return isConnectedNet; |
|
|
|
|
} |
|
|
|
|