Sunday, 15 March 2015

Android: Understanding using one IntentService with multiple actions -



Android: Understanding using one IntentService with multiple actions -

i have problem intent not getting delivered intentservice under specific activity flow: scenario:

consider 3 activities, home, b , c. c has 2 fragments cf1 , cf2. b, cf1 , cf2 utilize same intentservice class different actions. intentservice gets started using startservice(intent). (getactivity().startservice(intent) fragments) wherever intentservice gets started, create sure gets stopped using stopservice(intent) if running in activity/fragment's onstop(). if activity flow home -> c - > cf1 - >cf2, works. if activity flow home -> b - > c -> cf1 - > cf2, onhandleintent never invoked after startservice(intent) cf2. b , cf1 intents handled. debugging, tried waiting intentservice finish in activity b , go cf1 -> cf2, still same problem. cf1 never seems have problems in starting same intent service. when tried creating new intentservice class cf2, worked.

my understanding intentservice has queue intents. if service running first time , onstartcommand invoked (which not supposed handle intentservice). if service running, onhandleintent invoked every subsequent phone call of startservice.

obviously, doing wrong not clear what. have tried looking other stackoverflow questions didn't help. code using pretty straightforward:

androidmanifest.xml

<service android:name=".service.exampleintentservice" />

activity b

@override public void oncreate(bundle savedinstancestate) { ....... intent = new intent(getapplicationcontext(), exampleintentservice.class); intent.setaction(stringconstants.action_b); servicerunning = true; //set false in onreceiveresult startservice(intent); } @override public void onstop() { if(servicerunning && intent != null) stopservice(intent) }

fragment cf1

@override public void onresume() { super.onresume(); intent = new intent(getactivity(), exampleintentservice.class); intent.setaction(stringconstants.action_cf1); servicerunning = true; //set false in onreceiveresult startservice(intent); } @override public void onstop() { if(servicerunning && intent != null) stopservice(intent) }

the code same fragment, cf2

my understanding was... if service running first time , onstartcommand invoked (which not supposed handle intentservice). if service running, onhandleintent invoked every subsequent phone call of startservice.

no. onstartcommand() called every startservice() call. onhandleintent() called every startservice() phone call made intentservice, unless in onstartcommand() alter normal behavior.

intentservice gets started using startservice(intent).

you send commands intentservice using startservice().

wherever intentservice gets started, create sure gets stopped using stopservice(intent)

that exceptionally bad idea. intentservice stop when startservice() calls have been processed, mentioned in the documentation:

intentservice receive intents, launch worker thread, , stop service appropriate.

android intentservice

No comments:

Post a Comment