1

Can somebody tell me what the equivalent of this answer https://stackoverflow.com/a/27312494/9507009 without lambda expression, please ?

Extra: One-shot AsyncTask Example

class InternetCheck extends AsyncTask<Void,Void,Boolean> {

    private Consumer mConsumer;
    public  interface Consumer { void accept(Boolean internet); }

    public  InternetCheck(Consumer consumer) { mConsumer = consumer; execute(); }

    @Override protected Boolean doInBackground(Void... voids) { try {
        Socket sock = new Socket();
        sock.connect(new InetSocketAddress("8.8.8.8", 53), 1500);
        sock.close();
        return true;
    } catch (IOException e) { return false; } }

    @Override protected void onPostExecute(Boolean internet) { mConsumer.accept(internet); }
}

///////////////////////////////////////////////////////////////////////////////////
// Usage

    new InternetCheck(internet -> { /* do something with boolean response */ });
Roll'Froy
  • 13
  • 4

2 Answers2

1

Without lambda expression it should be like:

new InternetCheck(new InternetCheck.Consumer() {
    @Override
    public void accept(Boolean internet) {
        if (internet) {
            Log.d("TAG", "Internet is connected");
            doSomethingOnConnected();
        } else {
            Log.d("TAG", "Internet is not connected");
            doSomethingOnNoInternet();
        }     
    }
}).execute();
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • Thanks but how I do to get `internet` value out of this (sorry, I'm a beginner) ? – Roll'Froy Mar 17 '18 at 11:19
  • @Roll'Froy you just need to check if internet is true or false in the callback. It is an asynchronous method so it won't return immediately, you have to wait for the call back. – Nongthonbam Tonthoi Mar 17 '18 at 11:22
  • Understood but I want to launch a new activity if there is Internet (`startActivity(new Intent(this, MainActivity.class));`). Do I have to change the interface to pass the context in order to that ? – Roll'Froy Mar 18 '18 at 01:57
  • @Roll'Froy If you are doing startActivity in activity class, you can just do `startActivity (new Intent(MycurrentActivity.this, MainActivity.class));` – Nongthonbam Tonthoi Mar 18 '18 at 04:14
  • Yes, I finally got that, tanks. I put this in an splash screen activity and it's perfect. In your code of your answer, `.execute()` is too many because it's already in the constructor. – Roll'Froy Mar 18 '18 at 16:39
1
///////////////////////////////////////////////////////////////////////////////////
// Usage

    new InternetCheck(new InternetCheck.Consumer() {
        @Override
        public void accept(Boolean internet) { /* do something with boolean response */ } 
    });

... e.g. would be an equivalent non-lambda representation (as @NongthonbamTonthoi also mentioned).

Addition note to this code-section (as the author)

When using it in an activity context you might wanna check if your App/Activity is in "onPause/onStop"-state though, since the answer/call will be asynchronous and you might depend on it.

I did not mention this in the original answer, since it goes into too much detail, but setting a boolean to true in onResume, setting it to false in onPause, and checking it (e.g.) in our onPostExecute would be good practice in most cases.

(edit) More details / example:

boolean mStopped;

@Override
protected void onStart() {
    super.onStart();
    mStopped = false;
}

@Override
protected void onStop() {
    mStopped = true;
    super.onStop();
}

And in the simplest "just to be safe" way, check it within your accept-Method:

    new InternetCheck(new InternetCheck.Consumer() {
        @Override public void accept(Boolean internet) { 
            if (mStopped) return; 

            /* ... */ 
        } 
    });

OR check it within the AsyncTask (=InternetCheck class) itself (within onPostExecute!), which is obviously only possible if it resides in the same file/class, ...

In short, there are many ways to tackle this, including a custom (abstract) Actvity-class for that, which you have all your activities inherit from; a custom App-class that does something similar (dubious if done wrong!!); or using a ContentProvider (good practice, but a lot of work).

Community
  • 1
  • 1
Levite
  • 17,263
  • 8
  • 50
  • 50
  • Would you be able to explain in more detail how to do this check? Because sometimes I get the error while running this as an async task: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@b6a2dfa is not valid; is your activity running? – Adam S. Feb 11 '19 at 21:19
  • Thanks! That helps a lot in figuring things out. – Adam S. Feb 12 '19 at 00:39