android - start service from activity and wait until its ready -


i want start service activity.

first tried localbinder. works, service bound activity. don't want stop service when activity gone. found no solution localbinder removed , tried this:

use singleton instance in service call startservice methode in new thread , waits until instance available:

final intent recordservice = new intent(recordactivity.this, recordservice.class);     runnable r = new runnable() {         @override         public void run() {             startservice(recordservice);         }     };     new thread(r).start();      log.i(mmlf.t, "service instance: "+serviceinstance);     final progressdialog mprogressdialog = progressdialog.show(             recordactivity.this, "waiting", "wait until record service loaded",             true);     while (serviceinstance == null) {         serviceinstance = recordservice.get();         try {             thread.sleep(1000);         } catch (interruptedexception e) {             log.e(mmlf.t, "could not sleep", e);         }     }     mprogressdialog.dismiss(); 

but doesn't work, too. stucked in waiting loop. if remove waiting stuff , new new thread(r).start() line last, activity , service start fine.

how start service independent activity? let them communicate each other. activity should call 2 methods (start , stop recording) , service should send messages. second can use localbroadcast.

your question little confusing, because services live independently of activities. note, however, services run in main thread default. if want run service in different thread (and in case looks do), have set messenger object , send messages between worker thread , ui thread. can using aidl (on top of messenger built anyway). communication, if don't use messenger, use intents. if case, should intentservice. however, works when sending messages to service, not , forth. if want , forth communication, have use sort of messenger or similar pattern.

by way, using intentservice things 'stop' , 'start' pretty common. typically there background thread, communicates service using messenger or similar, , sends / receives messages instruct worker thread should done.

you might asynctask, makes kind of thing simpler.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -