Search code examples
androidfreezerunnable

Android app freezes without ANR after moving code into runnable


I'm implementing a custom camera and i use onPreviewFrame() for real-time fx. The main effect (fx1) is a runnable and seems to be ok, dropping frames when needed.
A second simpler fx (fx2) runs when fx1 is off.
When fx2 was running on the main thread was ok (it runs in 2msec) but sometimes i saw an ANR so i decided to move it to a second runnable, but now causing app to freeze.

class FX2Runnable implements Runnable {
    private byte[] data;
    FX2Runnable(byte[] _data) {
        data = _data;
    }

    public void run() {
        hview.FX2(data);

        hview.post(new Runnable() {
            public void run() {
                hview.postInvalidate();
                //.invalidate(); caused ANR!!
            }
        });
        FLAG_FX2_PROCESSING = false;
    }
}

here is how i call it

private PreviewCallback previewCallback=new PreviewCallback() {
    public void onPreviewFrame(byte[] data, Camera cam) {
        if(FX==1) {
            // FX1
            if(FLAG_FX1_PROCESSING) {
                //skip
            } else {
                FLAG_FX1_PROCESSING = true;
                FX1Thread = new Thread(new FX1Runnable());
                FX1Thread.start();
            }
        } else {
            // FX2
            if(FLAG_FX2_PROCESSING) {
                //skip
            } else {
                FLAG_FX2_PROCESSING = true;
                FX2Thread = new Thread(new FX2Runnable(data));
                FX2Thread.start();
            }
            /* this was the old code and was working fine
               but i needed to solve some spare ANR
            //hview.FX2(data);
            //hview.invalidate();
            */
        }
    }
}

What am i doing wrong!?

By the way, it seems i cannot freeze the app on the emulator, but only on my device.

EDIT

Tried with runOnUiThread(Runnable) instead of hview.post(Runnable), it seemed to work but i just had to wait some minutes to see freezing app again! It happens later but it happens again!

Here's a logcat

08-26 17:32:13.398: I/ActivityManager(250): Displayed it.jcsoft.abbracadabbra/.EnhancedCameraPreviewActivity: +3s576ms
08-26 17:32:13.414: W/IInputConnectionWrapper(459): showStatusIcon on inactive InputConnection
08-26 17:32:13.441: I/GPS(9149): new network location: Location[mProvider=network,mTime=1377531132668,mLatitude=44.1459721,mLongitude=12.4545174,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=43.887,mExtras=Bundle[mParcelledData.dataSize=212]]
08-26 17:32:15.441: D/dalvikvm(250): WAIT_FOR_CONCURRENT_GC blocked 0ms
08-26 17:32:15.867: D/dalvikvm(250): GC_EXPLICIT freed 1300K, 23% free 16030K/20615K, paused 3ms+41ms, total 428ms
08-26 17:32:37.707: D/dalvikvm(250): GC_CONCURRENT freed 1878K, 22% free 16105K/20615K, paused 4ms+110ms, total 888ms
08-26 17:32:37.855: D/dalvikvm(9149): null clazz in OP_INSTANCE_OF, single-stepping
08-26 17:32:49.375: D/dalvikvm(834): GC_CONCURRENT freed 512K, 9% free 9179K/9991K, paused 336ms+218ms, total 4454ms
08-26 17:32:49.375: D/dalvikvm(834): WAIT_FOR_CONCURRENT_GC blocked 1740ms
08-26 17:32:58.605: D/dalvikvm(250): GC_CONCURRENT freed 1934K, 22% free 16098K/20615K, paused 5ms+32ms, total 284ms
08-26 17:33:00.070: D/SizeAdaptiveLayout(335): com.android.internal.widget.SizeAdaptiveLayout@422dcb90child view android.widget.FrameLayout@422edb18 measured out of bounds at 95px clamped to 96px
08-26 17:33:00.093: D/SizeAdaptiveLayout(335): com.android.internal.widget.SizeAdaptiveLayout@4231b860child view android.widget.FrameLayout@4231d180 measured out of bounds at 95px clamped to 96px
08-26 17:33:10.257: D/dalvikvm(9149): GC_CONCURRENT freed 455K, 4% free 14709K/15303K, paused 3ms+4ms, total 78ms
08-26 17:33:20.082: D/dalvikvm(250): GC_CONCURRENT freed 1933K, 22% free 16091K/20615K, paused 5ms+30ms, total 282ms
08-26 17:33:23.379: I/InputDispatcher(250): Application is not responding: Window{425f23b0 it.jcsoft.abbracadabbra/it.jcsoft.abbracadabbra.EnhancedCameraPreviewActivity paused=false}.  It has been 5002.6ms since event, 5002.5ms since wait started.  Reason: Waiting because the touched window has not finished processing the input events that were previously delivered to it.
08-26 17:33:23.379: I/WindowManager(250): Input event dispatching timed out sending to it.jcsoft.abbracadabbra/it.jcsoft.abbracadabbra.EnhancedCameraPreviewActivity
08-26 17:33:28.437: I/InputDispatcher(250): Dropped event because it is stale.

please help!


Solution

  • You should really use a Handler for this kind of operation.

    Have the runnable post a message once it's done with it's operation and then do the hview.postInvalidate(); method call.

    There are tons of guides how to do it.

    Some links include: http://developer.android.com/reference/android/os/Handler.html

    http://androidexample.com/Thread_With_Handlers_-_Android_Example/index.php?view=article_discription&aid=58&aaid=83

    http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html