简体   繁体   English

使用循环程序使用自己的线程的Android Java对象

[英]Android Java object with own thread using looper

I have tried to implement object in Android that would work in its own thread (I do not want to make handler public, I want to wrap sentMessage method with own public api). 我试图在Android中实现可以在其自己的线程中工作的对象(我不想公开处理程序,我想用自己的公共api包装sendMessage方法)。 It has public methods to pass data to object. 它具有将数据传递给对象的公共方法。 This object is associated with activity lifecycle (onResume, onPause). 该对象与活动生命周期(onResume,onPause)相关联。 I would like to use looper and handler, not just pure Java thread with infinite loop. 我想使用循环器和处理程序,而不仅仅是具有无限循环的纯Java线程。 I want to start worker thread on resume and stop working on pause callback. 我想在恢复上启动工作线程,并停止在暂停回调上工作。 Object has to wait for new message infinitely. 对象必须无限等待新消息。 This is my code below: 这是我的代码如下:

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

Is it correct implementation? 实施正确吗? I receive warning: "sending message to a handler on a dead thread". 我收到警告:“在死线程上发送消息给处理程序”。 Is there any issue that I do not see? 有没有我看不到的问题?

It is my implementation: 这是我的实现:

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

In that run() method, if you need to re run or use loop, you need to add it by your self, ex. 在该run()方法中,如果需要重新运行或使用循环,则需要自行添加。

and your error, is happen because you try to call the thread that already finish. 发生错误是因为您尝试调用已经完成的线程。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM