[androidbrasil-dev] Comunicação Activity <-> Service

18 views
Skip to first unread message

Gink Labrev

unread,
Feb 18, 2015, 3:58:42 AM2/18/15
to androidb...@googlegroups.com
Pessoal,

Só para confirmar.
Posso fazer uma Activity referenciar um Service através de um Binder.
Mas não há como um Service referenciar a Activity ao qual este está atrelado. Para um Service passar dados para uma Activity, somente por Otto ou Broadcast. É isso ?

[]`s

Geovani **Leia a documentação oficial** de Souza

unread,
Feb 19, 2015, 12:33:49 PM2/19/15
to androidb...@googlegroups.com
Vc também pode criar um interface, fazer sua Activity implementar ela e usar a ServiceConnection para registrar/deregistrar a Activity no serviço, seguindo um Design Pattern Publisher & Subscriber (também conhecido como Observer).

Gink Labrev

unread,
Feb 19, 2015, 1:42:27 PM2/19/15
to androidb...@googlegroups.com
Obrigado pela resposta Geovani.
Mas não entendi bem seu ponto em relação ao ServiceConnection  
Uma instância de ServiceConnection é passado por parâmetro para o método bindService() da Activity.
Esse método retorna no callback onServiceConnected() o binder do Service. E atráves do binder, posso obter a referência ao serviço.

Não vejo aonde ou como registrar a Activity no Service no esquema acima.

2015-02-19 15:33 GMT-02:00 Geovani **Leia a documentação oficial** de Souza <geovani...@gmail.com>:
Vc também pode criar um interface, fazer sua Activity implementar ela e usar a ServiceConnection para registrar/deregistrar a Activity no serviço, seguindo um Design Pattern Publisher & Subscriber (também conhecido como Observer).

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gink Labrev

unread,
Feb 19, 2015, 1:50:58 PM2/19/15
to androidb...@googlegroups.com
Ah foi mal.
Não me liguei. Depois de obter a referência ao serviço, seria só chamar um método do tipo service.setActivity(this);
Valeu !!!

geovani...@gmail.com

unread,
Feb 19, 2015, 1:55:39 PM2/19/15
to androidb...@googlegroups.com
Adoro quando a resposta vêm sozinha! kkkkk

Só não esqueça de de-registrar sua Activity no onServiceDisconnected(), ativado através do Context#unbindService(), senão vc pode ser pego por NPE.

--
You received this message because you are subscribed to a topic in the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/androidbrasil-dev/o6WL1nebyGg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to androidbrasil-...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
@geovanisouza92 - Geovani de Souza

Gink Labrev

unread,
Feb 20, 2015, 12:49:39 PM2/20/15
to androidb...@googlegroups.com
Sim.
Valeu @geovanisouza92
Reply all
Reply to author
Forward
0 new messages