0

我试图在 Android 中实现可以在自己的线程中工作的对象(我不想公开处理程序,我想用自己的公共 api 包装 sendMessage 方法)。它具有将数据传递给对象的公共方法。该对象与活动生命周期(onResume、onPause)相关联。我想使用循环器和处理程序,而不仅仅是带有无限循环的纯 Java 线程。我想在恢复时启动工作线程并停止处理暂停回调。对象必须无限等待新消息。这是我下面的代码:

public class ThreadingObject {

    private MyThread thread;

    public ThreadingObject() {}

    public void onResume() {
        thread = new MyThread();
        thread.startWorking();
    }

    public void onPause() {
        thread.stopWorking();
    }

    public void setMessage(Object object) {
        Message msg = new Message();
        msg.obj = object;
        thread.handler.sendMessage(msg);
    }

    protected void work(Object object) {
        //Do something with object in own thread
    }

    private class MyThread extends Thread {
        public Handler handler;

        @Override
        public void run() {
            Looper.prepare();
            handler = new Handler() {

                @Override
                public void handleMessage(Message msg) {
                    ThreadingObject.this.work((String[]) msg.obj);
                }
            };
            Looper.loop();
        }

        public void startWorking() {
            start();
        }

        public void stopWorking() {
            handler.getLooper().quit();
        }
    }
}

是正确的实现吗?我收到警告:“向死线程上的处理程序发送消息”。有什么我看不到的问题吗?

4

2 回答 2

0

这是我的实现:

public class ThreadingObject {

    private HandlerThread thread;
    private Handler handler;

    private Handler.Callback handlerCallback = new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            work(msg.obj);
            return true;
        }
    };

    public ThreadingObject() {}

    public void onResume() {
        thread = new HandlerThread("SurfaceView HandlerThread");
        thread.start();
        handler = new Handler(thread.getLooper(), handlerCallback);
    }

    public void onPause() {
        if(thread != null) {
            thread.quit();
            thread = null;
        }
        handler = null;
    }

    public void setMessage(Object object) {
        Message msg = new Message();
        msg.obj = object;
        handler.sendMessage(msg);
    }

    protected void work(Object obj) {
        //Working
    }
}
于 2015-07-18T11:42:12.073 回答
-1

在那个run()方法中,如果你需要重新运行或者使用循环,你需要自己添加它,例如。

并且您的错误是因为您尝试调用已经完成的线程而发生的。

    boolean aLoopLoopLoop = true;
    Handler handler;

    // this handler, you no need to declare it repeatedly, just declare it in onCreate() is enough
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            ThreadingObject.this.work((String[]) msg.obj);
        }
    };

    // -----
    @Override
    public void run() {
        Looper.prepare();
        final Looper looper = Looper.myLooper();
        while(aLoopLoopLoop) {
            // write your code here
            Looper.loop();
        }
        // after loop exit, quit loop
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                looper.quit(); 
            }
        }, 3000);
    }

    // -----
    public void stopWorking() {
        aLoopLoopLoop = false;
    }
于 2015-07-17T14:10:02.517 回答