service -> activity communication

Put your problem here if it does not fit any of the other categories.

service -> activity communication

Postby dpmancus » Sun Nov 16, 2008 7:21 pm

hi to everybody,

in my application there is a service that start a thread to listen to incoming socket messages.
how can i notify my activity that a message is arrived?

regards,
dpmancus
dpmancus
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 15, 2008 10:39 pm
Location: Italy

Top

Postby MrSnowflake » Sun Nov 16, 2008 9:04 pm

You should make and register listeners which you service can call.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby dpmancus » Mon Nov 17, 2008 1:52 am

MrSnowflake wrote:You should make and register listeners which you service can call.


can you give me an example of your idea on my app please???

this is moreless how it is structured:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class myActivity extends Activity
  2. {
  3.     public void onCreate()
  4.     {
  5.         startService();
  6.         .....
  7.     }
  8. }
  9.  
  10.  
  11. public class myService extends Service
  12. {
  13.     public void onStart()
  14.     {
  15.          new Thread(){
  16.              public void run(){
  17.                 ....
  18.                 while(true){
  19.                       String meg = (String)ios.readObject();
  20.                       // how can tell the activity about the incoming message????
  21.                 }
  22.             }
  23.         }.start();
  24.     }
  25. }
  26.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
dpmancus
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 15, 2008 10:39 pm
Location: Italy

Postby MrSnowflake » Mon Nov 17, 2008 11:49 am

Push a listener object to the service which the thread will call. This is basic event handling (check Swing GUI tutorials):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. //In Listener.aidl
  3.  
  4. interface Listener {
  5.  
  6. void onMessageArrived();
  7.  
  8. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

In your acitivy:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...
  2.  
  3. Listener messageListener = new Listener() {
  4.  
  5. void onMessageArrived() {
  6.  
  7. //Do some stuff
  8.  
  9. // But if you want UI stuff use Acitivity.runOnUiThread();
  10.  
  11. }
  12.  
  13. }
  14.  
  15.  
  16.  
  17. mService.registerListener(messageListener);
  18.  
  19. // registerListener should be in you services aidl.
  20.  
  21.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

The Service keeps a record of the Listener
And when the thread senses a new message:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mParentService.fireOnMessage();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby dpmancus » Mon Nov 17, 2008 12:38 pm

i know the listner philosophy, i understood you advice but i still dont know how the acivity has got an istance of the service.
dpmancus
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 15, 2008 10:39 pm
Location: Italy

Postby MrSnowflake » Mon Nov 17, 2008 1:06 pm

Why don't you check the docs. They explain everything there is to know about the Service?!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby dpmancus » Mon Nov 17, 2008 2:24 pm

i read documentation...

my goal is that when a message arrives a view(a TextView) of myActivity has to change.
in my app i cant bind the service from myActivity, i must call startService() so, how can the activity got an istance of myService???
dpmancus
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 15, 2008 10:39 pm
Location: Italy

Postby MrSnowflake » Mon Nov 17, 2008 4:27 pm

For such functionality you MUST bind to the service, or setup some strange server client stuff using the service and activity, but then the binding is a __LOT__ better.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby dpmancus » Tue Nov 18, 2008 4:53 pm

MrSnowflake wrote:For such functionality you MUST bind to the service, or setup some strange server client stuff using the service and activity, but then the binding is a __LOT__ better.


I've just solved my problem still using startService() - obviously a local service. I used the pattern singleton and the observer one:

myService is the subject for mySingleton,
mySingleton is the subject for myActivity.

mySingleton is at the same time subject and listener.

myService and myActivity get the mySingleton only-instance from the static method getInstance()

in this way the service can update a view of the activity
dpmancus
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 15, 2008 10:39 pm
Location: Italy

Postby MrSnowflake » Tue Nov 18, 2008 6:53 pm

That is indeed a nice workaround, using a Singleton. But keep in mind, this only works for classes within 1 package.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby dpmancus » Tue Nov 18, 2008 7:32 pm

ok... i'll keep it in mind!!
dpmancus
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 15, 2008 10:39 pm
Location: Italy

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 20 guests