android - How can I jump from a Handler started from a service to the service's thread? -
i use handler in service class background work, when it's done, must execute code on service's thread. here's code:
i create handler in service's oncreate method.
handlerthread handlerthread = new handlerthread(getpackagename()); handlerthread.start(); looper looper = handlerthread.getlooper(); mhandler = new handler(looper);
how use handler. method getting called dozens of times during session.
mhandler.post(new runnable() { @override public void run() { // takes few seconds execute method, // must running on separate thread. object o = supermethod(); // however, must called service's thread. useobject(o); } };)
so how can service's thread?
service runs on main thread here want switch handler thread(background thread) service(main thread).
handler handler = new handler(getmainlooper()); handler .post(new runnable() { @override public void run() { // main thread task here } });
Comments
Post a Comment