首先,您必须声明一个方法来检查是否有互联网连接。
使用ConnectivityManager
和NetworkInfo
public static boolean hasInternetConnection(final Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
return false;
} else
return true;
}
然后...
try{
if(Util.hasInternetConnection(mcontext)){
//todo
@Override
public void onSuccess(String message) {
showAlertDialog("Alert",message);
}
@Override
public void onFail(String errorcode, String errormessage) {
showAlertDialog("Alert",errormessage);
}
});
}else{
showInternetRequiredDialog(getString(R.string.title_internet_require), getString(R.string.msg_no_internet_connection_setup));
return;
}
}catch (Exception ex){
ex.printStackTrace();
}
至于你想如何写你的检查逻辑,我会留给你