1

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();
}
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • 1
    Could you give more explanation why do you need to sleep main thread? – Eugen Martynov Jun 15 '14 at 13:21
  • You can disable UI elements with good explanaiton to user and do not block main thread – Eugen Martynov Jun 15 '14 at 13:22
  • 2
    Whatever you are trying to solve this way can be better solved by some other means. – CommonsWare Jun 15 '14 at 13:23
  • @EugenMartynov I want the app to wait for a certain amount of time, to see if some processing gives it the desired results withing that time or it requires more time. and also I don't want the user to make any interactions with the app within that time. – Muhammed Refaat Jun 15 '14 at 13:24
  • 1
    So you can show progress indicator with explanation "Processing..." and disable UI for required duration – Eugen Martynov Jun 15 '14 at 13:25
  • @CommonsWare I explained in the previous comment why I'm doing that, If you can provide me with a better way according to my needs, kindly refer to it – Muhammed Refaat Jun 15 '14 at 13:26
  • @EugenMartynov i thinked about that but I don't want to give the user that kind of message in the stage of my app. for sure this way is considered but as a plan B – Muhammed Refaat Jun 15 '14 at 13:28
  • @MuhammedRefaat please realise SO is online platform so people don't see comments and answers at the same time – Eugen Martynov Jun 15 '14 at 13:28
  • 1
    Its not a good idea to put main thread to sleep. Check out http://stackoverflow.com/a/6092143/254567 . If still you wanna do that, you may try disabling the touch event on your activity before putting it to sleep. Check this out- http://stackoverflow.com/a/10721034/254567 – epiphany27 Jun 15 '14 at 13:28
  • "I don't want to give the user that kind of message in the stage of my app" -- you would prefer to frustrate the users, because they try using your app and it does not respond to user input, with no explanation of why? – CommonsWare Jun 15 '14 at 13:36
  • @PushkarPandey I tried the way to disable the touch event without success. – Muhammed Refaat Jun 15 '14 at 13:59
  • @CommonsWare I have a please wait.. text view, i just don't want to raise dialogs in that stage of the app – Muhammed Refaat Jun 15 '14 at 14:03

1 Answers1

3

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".

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491