简体   繁体   中英

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). It has public methods to pass data to object. This object is associated with activity lifecycle (onResume, onPause). I would like to use looper and handler, not just pure Java thread with infinite loop. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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