Android - Key Dispatching Timed Out

寵の児 提交于 2019-11-26 16:32:20
Alex

You must be as fast as possible in your onClick implementation. Expensive operations should be, in general, offloaded to a background thread.

In onClick, try:

Thread t = new Thread(){
    public void run(){
        your_stuff();
    }
};
t.start();

instead of just

your_stuff()

You can encounter this error when you block the main thread (a.k.a. the UI thread) for a few seconds. Expensive operations should be, in general, offloaded to a background thread. AsyncTask is very helpful in these cases.

In your case you could do the following:

new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        try {
            serviceBinder.endCall(lineId);
        } catch (RemoteException e) {
            e.printStackTrace();
        } 
    }
}.execute();
Xar E Ahmer

Do your long Operation in a seperate thread or use AsyncTask to get rid of ANR.

An ANR(Activity Not Responding) happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog.

Your activity took to long to say to the Android OS 'hey i'm still alive'! (This is what the UI thread does).

http://developer.android.com/guide/practices/design/responsiveness.html

Basically if you make the UI thread do some complex task it's too busy doing your task to tell the OS that it is still 'alive'.

http://android-developers.blogspot.co.uk/2009/05/painless-threading.html

You should move your XML Parsing code to another thread, then use a callback to tell the UI thread you have finished and to do something with the result.

http://developer.android.com/resources/articles/timed-ui-updates.html

Detecting where ANRs happen is easy if it is a permanent block (deadlock acquiring some locks for instance), but harder if it's just a temporary delay. First, go over your code and look for vunerable spots and long running operations. Examples may include using sockets, locks, thread sleeps, and other blocking operations from within the event thread. You should make sure these all happen in separate threads. If nothing seems the problem, use DDMS and enable the thread view. This shows all the threads in your application similar to the trace you have. Reproduce the ANR, and refresh the main thread at the same time. That should show you precisely whats going on at the time of the ANR

If Logcat doesn't output anything usefull, try to pull traces.txt from /data/anr/traces.txt

adb pull /data/anr/traces.txt .

as it may give more information on where the ANR Exception occurrred

And this link may also helpful for creating AsyncTask and Threads

If you are doing a resource intensive task then it might happen. While resuming the Activity. 1. Try stopping all your intensive work on onPause and then restarting it on onResume. 2. If you are showing map on Activity drawing overlay on it then stop refreshing the overlays while on sleep. And then restart it on onResume.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!