1

I'm trying to create an app that checks for internet connection when it opens. I want it to display a loading screen as long as there is not internet connection and a message. The loading screen activity is activity_main.xml. The problem is that because I'm calling LoggingIn method from itself, it keeps repeating it untill I have internet connection, but the problem is that for some reason it just won't load the activity itself. It just shows me a blank screen. When I don't run LoggingIn the activity does work.

Please help is there any other way to do this?

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LoggingIn();
}

public void LoggingIn ()
{
    if (isNetworkAvailable())
    {
        if (findViewById(R.id.InternetConnection).getVisibility() == View.VISIBLE)
        {
            findViewById(R.id.InternetConnection).setVisibility(View.GONE);
        }
        AttemptLoggingIn();
    }
    else
    {
        findViewById(R.id.InternetConnection).setVisibility(View.VISIBLE);
        LoggingIn();


    }
}
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
morha13
  • 1,847
  • 3
  • 21
  • 42
  • I encourage you to read [this](https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html). You can use a callback instead of checking it in your way. Cheers – Augusto Carmo Aug 28 '16 at 12:09
  • What the hell is this ??? Current checking? – Vyacheslav Aug 28 '16 at 12:09
  • @AugustoCarmo what do you mean use callback? I mean I still want to check for the internet connection constantly, how using callback will help me in this case? Thank you for your time! – morha13 Aug 28 '16 at 12:13
  • Please, take a look at [this post](http://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app) – Augusto Carmo Aug 28 '16 at 12:15

2 Answers2

1

Use background threads instead of your very strange method.

For example,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startCheck();
}


    private boolean startCheck() {
        new Thread(new Runnable() {

                        @Override
                        public void run() {
        runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                          findViewById(R.id.InternetConnection).setVisibility(View.VISIBLE);
                    }
                });

        while(!isNetworkAvailable()) {

        try {
                                    Thread.sleep(100L);// 100 ms sleep
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
        }
        runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (findViewById(R.id.InternetConnection).getVisibility() == View.VISIBLE)
                {
                    findViewById(R.id.InternetConnection).setVisibility(View.GONE);
                }
                AttemptLoggingIn();
                    }
                });

        }
                    }).start();
    }
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • Thank you so much! I still don't know what you did here but it fixed it! Now I'm gonna read what you did here :P – morha13 Aug 28 '16 at 12:18
  • Lol. The idea is to start another thread which touches user interface only when the network is changed – Vyacheslav Aug 28 '16 at 12:27
  • So basically it keeps doing the loop in the middle as long as there is no internet connection, and when there is it continues to the rest of the code? – morha13 Aug 28 '16 at 12:29
0

You don't need to call LoggingIn() to monitor network changes.

ConnectivityManager automatically broadcasts the CONNECTIVITY_ACTION action whenever the connectivity details change.This article can be really useful for you:

http://www.androidhive.info/2012/07/android-detect-internet-connection-status/