最好使用HandlerThread比其他相似的类 [英] Best use of HandlerThread over other similar classes

查看:119
本文介绍了最好使用HandlerThread比其他相似的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道最好的用例使用 HandlerThread

I am trying to understand the best use case of using HandlerThread.

根据定义:

手持类为起始,其具有尺蠖一个新线程。活套随后可以用来创建处理程序的类。注意,开始()必须仍然被调用。

"Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called."

我可能是错的,但类似的功能,我可以使用尺蠖和<$ C $实现C>处理程序。所以,什么时候应该使用 HandlerThread ?一个例子是真正的帮助。

I may be wrong but similar functionality I can achieve by using a Thread, Looper and Handler. So when should I use HandlerThread? An example would be really helpful.

推荐答案

下面是一个活生生的例子,其中 HandlerThread 变得得心应手。当您注册相机preVIEW框架,您会收到他们的在previewFrame()回调。该<一href="http://developer.android.com/reference/android/hardware/Camera.$p$pviewCallback.html">documentation解释说,这个回调是在事件线程开(int)的调用由名为

Here is a real life example where HandlerThread becomes handy. When you register for Camera preview frames, you receive them in onPreviewFrame() callback. The documentation explains that This callback is invoked on the event thread open(int) was called from.

通常,这意味着该回调将主(UI)线程上调用。因此,处理庞大的像素阵列的任务可能会被卡住时,菜单打开,动画是动画,或者即使显示在屏幕上。统计

Usually, this means that the callback will be invoked on the main (UI) thread. Thus, the task of dealing with the huge pixel arrays may get stuck when menus are opened, animations are animated, or even if statistics in printed on the screen.

最简单的解决方案是创建一个新HandlerThread()和委托 Camera.open()此主题(我通过后做了(Runnable接口),你并不需要实现 Handler.Callback )。

The easy solution is to create a new HandlerThread() and delegate Camera.open() to this thread (I did it through post(Runnable), you don't need to implement Handler.Callback).

请注意,所有其他工作着的摄像头可以做到像往常一样,你不必委托 Camera.start preVIEW() Camera.set previewCallback()的HandlerThread。为了安全起见,我的实际 Camera.open(INT)来完成,然后我继续在主线程(或任何线程被用来调用 Camera.open()更改之前)。

Note that all other work with the Camera can be done as usual, you don't have to delegate Camera.startPreview() or Camera.setPreviewCallback() to the HandlerThread. To be on the safe side, I wait for the actual Camera.open(int) to complete before I continue on the main thread (or whatever thread was used to call Camera.open() before the change).

所以,如果你开始与code

So, if you start with code

try {
    mCamera = Camera.open(1);
}
catch (RuntimeException e) {
    Log.e(LOG_TAG, "failed to open front camera");
}

先提取它的 的是 成一个私有方法:

first extract it as is into a private method:

private void oldOpenCamera() {
    try {
        mCamera = Camera.open(1);
    }
    catch (RuntimeException e) {
        Log.e(LOG_TAG, "failed to open front camera");
    }
}

和,而不是调用 oldOpenCamera()只需使用 newOpencamera()

and instead of calling oldOpenCamera() simply use newOpencamera():

private void newOpenCamera() {
    if (mThread == null) {
        mThread = new CameraHandlerThread();
    }

    synchronized (mThread) {
        mThread.openCamera();
    }
}
private CameraHandlerThread mThread = null;
private static class CameraHandlerThread extends HandlerThread {
    Handler mHandler = null;

    CameraHandlerThread() {
        super("CameraHandlerThread");
        start();
        mHandler = new Handler(getLooper());
    }

    synchronized void notifyCameraOpened() {
        notify();
    }

    void openCamera() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                oldOpenCamera();
                notifyCameraOpened();
            }
        });
        try {
            wait();
        }
        catch (InterruptedException e) {
            Log.w(LOG_TAG, "wait was interrupted");
        }
    }
}

更新:在这里,同样的方法也适用于加速: Acclerometer传感器在单独的线程

Update: Here the same approach is applied to accelerometer: Acclerometer Sensor in Separate Thread

这篇关于最好使用HandlerThread比其他相似的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆