M
M
Madion2015-03-20 08:19:20
Android
Madion, 2015-03-20 08:19:20

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;
    }

In the onStart () method in MainActivity, I check if there is an Internet connection:
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();
        }

When you first start the application through the emulator, it regularly shows that there is no Internet connection. But as soon as I exit the application and open it again through the menu, the dialog box does not pop up!
When I check on the phone - in general, it regularly gives out that there is no Internet connection. Although the Internet is 100% - there is.
In the file: Android.Manifest.xml, lines are added before the application tag:
<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

1 answer(s)
O
one pavel, 2015-03-20
@onepavel

After similar problems with the emulator, I bought my first android.
I don’t know how it is now, but earlier there was no Wi-Fi in emulators.
And the mobile Internet was regulated from AVD and it was a torment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question