Android/Java - Wait for AsyncTask to end before performing next function

Swap

I'm creating an app which'll show a list of configured Wi-Fi networks in its MainActivity. Now, before fetching the list of configured networks, my app checks if Wi-Fi is turned on. If not, it shows an AlertDialog containing a "Turn on Wi-Fi" Button which triggers an AsyncTask that turns on the Wi-Fi.

Now, I'm using this code to fetch the list of configured Wi-Fi networks

@SuppressWarnings("deprecation")
private ArrayList<String> getAvailableNetworks() {

    manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    availableNetworks = new ArrayList<String>();

    if (!manager.isWifiEnabled()) {
        showDialog(2);
    }

    if (manager.isWifiEnabled()) {

        configuredNetworks = manager.getConfiguredNetworks();

        for (int i = 0; i < configuredNetworks.size(); i++) {
                    availableNetworks.add(configuredNetworks.get(i).SSID.replaceAll("\"", ""));
        }
    }

    return availableNetworks;
}

Now, the problem is that when the app finds out that the Wi-Fi is turned off, it just shows the AlertDialog using showDialog(2); and moves on to the next line of code which leaves the list of configured networks completely blank.

How do I get it to wait until the AsyncTask finishes turning on Wi-Fi before it moves on to the next line of code?

Thanks in Advance.

Sarthak Mittal

You can start an Indeterminate Progress Dialog (unCancelable) at OnPreExecute and then dismiss it in onPostExecute

And if you don't want the code to get started executing before the asyncTask gets Completed, just put that code in onPostExecute method of AsyncTask

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

make jquery wait before performing the next action

From Dev

make jquery wait before performing the next action

From Dev

How to wait for loading of page before performing the next operation?

From Dev

Wait for an async function to end before executing another

From Dev

Wait for a task to end before executing the next code lines

From Dev

Wait for All Fades in a Function to Finish Before the Next Function Starts

From Dev

Wait for All Fades in a Function to Finish Before the Next Function Starts

From Dev

How can one wait for a function to complete before the next function is called?

From Dev

Ajax to wait until Api Is done before next function is executed

From Dev

wait for async function in loop to finish executing before next iteration

From Dev

Javascript: wait for function in loop to finish executing before next iteration

From Dev

Node js for-loop wait for asynchronous function before next iteration?

From Dev

How to wait for a function to end on iOS/Swift, before starting the second one

From Dev

Execute a function on the end of asynctask

From Dev

Wait for AsyncTask to finish before looping array

From Dev

Wait for a function to end

From Dev

How to wait for a binding to get its value before performing a task

From Dev

Make my program wait for a second before performing an action

From Dev

Performing methods in click() function before href

From Dev

Is there an alternative way, aside from sleep(), to make an AutoIt script wait before executing the next function?

From Dev

Javascript doesn't wait for STRIPE.COM function to finish before executing next line

From Dev

Wait until mongoose has saved to DB before calling the next await function?

From Dev

Resolve doesn't wait for API call to complete before falling to end of function

From Dev

Wait for completion before executing next step

From Dev

wait() before next iteration in a for loop java

From Dev

Make Mocha wait before running next test

From Dev

Protractor - Wait for async promise before doing next

From Dev

protractor - wait for previous it block to finish before next

From Java

Wait before calling a function in Swift

Related Related

  1. 1

    make jquery wait before performing the next action

  2. 2

    make jquery wait before performing the next action

  3. 3

    How to wait for loading of page before performing the next operation?

  4. 4

    Wait for an async function to end before executing another

  5. 5

    Wait for a task to end before executing the next code lines

  6. 6

    Wait for All Fades in a Function to Finish Before the Next Function Starts

  7. 7

    Wait for All Fades in a Function to Finish Before the Next Function Starts

  8. 8

    How can one wait for a function to complete before the next function is called?

  9. 9

    Ajax to wait until Api Is done before next function is executed

  10. 10

    wait for async function in loop to finish executing before next iteration

  11. 11

    Javascript: wait for function in loop to finish executing before next iteration

  12. 12

    Node js for-loop wait for asynchronous function before next iteration?

  13. 13

    How to wait for a function to end on iOS/Swift, before starting the second one

  14. 14

    Execute a function on the end of asynctask

  15. 15

    Wait for AsyncTask to finish before looping array

  16. 16

    Wait for a function to end

  17. 17

    How to wait for a binding to get its value before performing a task

  18. 18

    Make my program wait for a second before performing an action

  19. 19

    Performing methods in click() function before href

  20. 20

    Is there an alternative way, aside from sleep(), to make an AutoIt script wait before executing the next function?

  21. 21

    Javascript doesn't wait for STRIPE.COM function to finish before executing next line

  22. 22

    Wait until mongoose has saved to DB before calling the next await function?

  23. 23

    Resolve doesn't wait for API call to complete before falling to end of function

  24. 24

    Wait for completion before executing next step

  25. 25

    wait() before next iteration in a for loop java

  26. 26

    Make Mocha wait before running next test

  27. 27

    Protractor - Wait for async promise before doing next

  28. 28

    protractor - wait for previous it block to finish before next

  29. 29

    Wait before calling a function in Swift

HotTag

Archive