Answer the question
In order to leave comments, you need to log in
Why can it periodically issue that there is no Internet connection in the Android application?
Evening at home!
I recently started learning Android programming on my own. Faced such a problem.
There is a method:
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
if(this.isOnline() == true) {
//здесь идет произвольный код
} else {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setTitle("Ошибка.");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setMessage("Отсутствует интернет-соединение");
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question