Search code examples
multithreadingandroidthread-sleep

making the main Thread sleeps


I'm designing an android app that requires me to make the main Thread sleeps for a certain amount of time, I know that this will make the app freeze and the UI will be not an editable material, BUT, that's what I need in my app and I'm fine with that, the problem is, when the user consequently hitting the UI while the main Thread is sleeping, the app crashes and gives me the following Log:

06-15 13:10:57.620: E/ActivityManager(542): ANR in com.example.myapp (com.example.myapp/.Login)
06-15 13:10:57.620: E/ActivityManager(542): PID: 1339
06-15 13:10:57.620: E/ActivityManager(542): Reason: Input dispatching timed out (Waiting because the touched window has not finished processing the input events that were previously delivered to it.)
06-15 13:10:57.620: E/ActivityManager(542): Load: 0.67 / 0.36 / 0.14
06-15 13:10:57.620: E/ActivityManager(542): CPU usage from 8638ms to 2834ms ago:
06-15 13:10:57.620: E/ActivityManager(542):   2.7% 112/mediaserver: 0.1% user + 2.5% kernel
06-15 13:10:57.620: E/ActivityManager(542):   1.2% 542/system_server: 0.6% user + 0.5% kernel / faults: 10 minor
06-15 13:10:57.620: E/ActivityManager(542):   1% 100/vinput: 0% user + 1% kernel
06-15 13:10:57.620: E/ActivityManager(542):   1% 1339/com.example.myapp: 0.8% user + 0.1% kernel / faults: 19 minor
06-15 13:10:57.620: E/ActivityManager(542):   0.5% 102/local_opengl: 0% user + 0.5% kernel
06-15 13:10:57.620: E/ActivityManager(542):   0.3% 109/surfaceflinger: 0.3% user + 0% kernel
06-15 13:10:57.620: E/ActivityManager(542):   0% 3/ksoftirqd/0: 0% user + 0% kernel
06-15 13:10:57.620: E/ActivityManager(542):   0.1% 56/adbd: 0% user + 0.1% kernel
06-15 13:10:57.620: E/ActivityManager(542):   0% 103/local_gps: 0% user + 0% kernel / faults: 4 minor
06-15 13:10:57.620: E/ActivityManager(542):   0.1% 598/com.android.systemui: 0.1% user + 0% kernel / faults: 1 minor
06-15 13:10:57.620: E/ActivityManager(542):   0% 697/com.android.inputmethod.latin: 0% user + 0% kernel / faults: 5 minor
06-15 13:10:57.620: E/ActivityManager(542): 4% TOTAL: 1.4% user + 2.5% kernel
06-15 13:10:57.620: E/ActivityManager(542): CPU usage from 696ms to 1199ms later:
06-15 13:10:57.620: E/ActivityManager(542):   1.7% 100/vinput: 0% user + 1.7% kernel
06-15 13:10:57.620: E/ActivityManager(542): 2% TOTAL: 2% user + 0% kernel

How can I preventing making my app ignore any user interactions while sleeping?

if it's worth to mention, I'm catching the InterruptedException while making the Thread sleeping:

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

Solution

  • Call setEnabled(false) on the relevant widgets and action bar items to prevent them from accepting user input. Then, run your long-running work in the background, such as via an AsyncTask. Call setEnabled(true) on the relevant widgets and action bar items when the work is done, such as in onPostExecute() of the AsyncTask.

    Note that you will need to deal with configuration changes, so that you re-disable the widgets and action bar items if the user rotates the screen while your background work is going on.

    Also, please only disable the widgets and action bar items that are truly relevant to your background task. Do not block the user from navigating elsewhere in your app to places that do not depend upon this work (help screen, about screen, etc.).

    You also may be better served using a ProgressBar instead of, or in addition to, your "please wait.. text view".