multithreading - Android Background Service and Thread -
i developing android application using rabbit mq pub/sub technology. want listen incoming message in android background service. can run thread in android background service ?
public class messagingservice extends service { private thread subscribethread; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { super.oncreate(); subscribethread = new thread(new runnable() { @override public void run() { //connecting server , listen incoming message. } }); subscribethread.start(); } @override public int onstartcommand(intent intent, int flags, int startid) { return start_sticky; } @override public void ondestroy() { super.ondestroy(); subscribethread.interrupt(); } }
yes can run new thread in android service.
please see note in documentation here: http://developer.android.com/guide/components/services.html
caution: service runs in main thread of hosting process—the service not create own thread , not run in separate process (unless specify otherwise). means that, if service going cpu intensive work or blocking operations (such mp3 playback or networking), should create new thread within service work.
hope you.
Comments
Post a Comment