将代码移动到runnable后,Android应用程序在没有ANR的情况下冻结

时间:2013-08-26 11:55:07

标签: android freeze runnable

我正在实现自定义相机,我使用onPreviewFrame()进行实时fx。主要效果(fx1)是可运行的,似乎没问题,需要时丢帧 当fx1关闭时,第二个更简单的fx(fx2)运行 当fx2在主线程上运行时(它在2毫秒内运行)但有时我看到了一个ANR所以我决定将它移动到第二个runnable,但现在导致app冻结。

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;
    }
}

这就是我怎么称呼它

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();
            */
        }
    }
}

我做错了什么!?

顺便说一下,我似乎无法冻结模拟器上的应用程序,只能在我的设备上冻结。

修改

尝试使用runOnUiThread(Runnable)而不是hview.post(Runnable),它似乎工作但我只需要等待几分钟才能看到冻结应用程序!它发生在以后但又发生了!

这是一个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.

请帮忙!

2 个答案:

答案 0 :(得分:1)

请尝试使用hview.post...

,而不是使用runOnUiThread(new Runnable()...

在Android文档中,有以下单词。

  

在UI线程上运行指定的操作。如果当前线程是UI线程,则立即执行该操作。如果当前线程不是UI线程,则该操作将发布到UI线程的事件队列中。

因此,每当您想要从线程更新视图时,都需要使用runOnUiThread

所有视图都在UIThread上呈现。如果你不这样做,它会崩溃,因为后台线程与UIThread不同。

答案 1 :(得分:0)

您应该使用Handler进行此类操作。

让runnable发布一条消息,一旦它完成了它的操作,然后执行hview.postInvalidate();方法调用。

有很多指南如何做。

一些链接包括: 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