android - Service, Observer, and Loopers -


i trying create application contains content observers in remote service kept getting error:

"can’t create handler inside thread has not called looper prepare"

i have done reading on android developers site many google searches still not understanding problem. i've taken far content observer needs thread run on (for handler). after adding:

looper.mylooper().prepare();
mobserver = new observer(new handler());

a looper call before creating new content observer makes application work correctly. better understanding of why - nothing have found online has me grasp why far. also, adding looper line of code correct , efficient thing do? , there cleanup need prevent memory leaks later down road?

the simpler explanation better. still in first month of android development , making progress - stackoverflow has been tremendous help! thanks.

update
after playing around , doing more reading think have better grasp on how make work , how clean still missing 1 small part.

from i've gathered can call looper.prepare() static method. can create content observer using new handler(). when want stop content observer can thread of object mlooper = looper.mylooper.getthread() , can run mlooper.quit(). must , when , why call mlooper.loop()?

update 2
okay here problem addressing. application in house , little complicated explain here best shot:

we have remote service running receives , sends network data. our message types defined in data can direct them correct database on phone. there jar interface file other apk's import connect service running. these apk's can register receive broadcasts when new message interested in received. content observer registered content uri apk uses when new data added database content observer send out broadcast alert correct apk. apk can handle wishes.

so okay use null parameter of new content observer or better use new handler() create thread observer.

i have tried creating thread far not working, think because have not started looper.loop (this freezes service). advice appreciated! in advance.

when start looper in thread, other threads gain ability send messages , receive , process them. scheme used in android: looper initialized on ui thread , other threads assigned perform lenghty operations. 'worker' threads can send results of hard labor ui thread posting messages it's handler.

and, yes, need call looper.loop() in order things running. otherwise handler never receive events. , seems you'll have invoke looper.quit() in order stop , avoid leaking resources.

and here's funny part. judging contentobserver's source code, not require handler. if pass null, execute onchange() on same thread on runs.

p.s. dev machine down have no way verify statements. maybe current version of contentobserver require handler work. experiment show.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -