Service data exchange problem

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

Service data exchange problem

Postby Meleagros » Wed Sep 17, 2008 12:06 am

Hi all,

i tried to use a class implements the singleton pattern inside an android service. But it didn't get the right instance. Inside activities it works well.

I want to use the service to handle all incomming messages from a jabber server.
If i connect to the jabber server inside a activity i can use the singleton class in all activitys. but not in the service. if i connect inside the service, the activitys dont get the right instance of the singleton.

i tried to implement the serializable interface und put it in a my singleton class in a bundle, an then deliver it to the service. but then i get an error.

another try was to use ja final static HashMap variable. to exchange the class. but that also didn't work.



i am thankful for any ideas to solve this problem...

ps: is use the adapted smack api.
Meleagros
Freshman
Freshman
 
Posts: 7
Joined: Tue Sep 02, 2008 9:28 pm

Top

Re: Service data exchange problem

Postby rmcavalcante » Wed Sep 17, 2008 12:38 pm

Meleagros wrote:Hi all,

i tried to use a class implements the singleton pattern inside an android service. But it didn't get the right instance. Inside activities it works well.

I want to use the service to handle all incomming messages from a jabber server.
If i connect to the jabber server inside a activity i can use the singleton class in all activitys. but not in the service. if i connect inside the service, the activitys dont get the right instance of the singleton.

i tried to implement the serializable interface und put it in a my singleton class in a bundle, an then deliver it to the service. but then i get an error.

another try was to use ja final static HashMap variable. to exchange the class. but that also didn't work.



i am thankful for any ideas to solve this problem...

ps: is use the adapted smack api.


Hi Meleagros,

Since you're using a Service and you're interested in handling messages, perhaps you should try to send a callback to your Activity so the Activity will be able to perform the viewer role.
If you need any examples on how to handle callbacks I advise you to take look at the ApiDemos Android eclipse project the comes together with the Android SDK.
com.android.samples.app.RemoteServiceBinding has the example for handling callbacks.
For more information on how to define a callback message, take a look in Android AIDL.
Hope it helps.
rmcavalcante
Freshman
Freshman
 
Posts: 7
Joined: Thu Sep 11, 2008 10:48 am

Postby Meleagros » Wed Sep 17, 2008 12:54 pm

hi rmcavalcante,

thanks for your answer. but the callbacks are not my problem. there already implementet and work well.

simplified: i want to use the smack api connection in the activities and inside the service. but now i can use the api only in the activities or in the service. if i connect to the server inside the activities i cant use the connection inside the srevice and vice versa.

you can't use the callBack to deliver the xmpp connection. the datatypes are restricted you can use.

antother try that didn't work:


my jabber class implements android.app.Application und i get it with
Jabber myJabber = (Jabber)getApplication();

the connection is there a static field. but here is the same problem...

The android.app.Application class
The android.app.Application is a base class for those who need to maintain global application state. It can be accessed via getApplication() from any Activity or Service. It has a couple of life-cycle methods and will be instantiated by Android automatically if your register it in AndroidManifest.xml.
http://code.google.com/android/kb/framework.html
Meleagros
Freshman
Freshman
 
Posts: 7
Joined: Tue Sep 02, 2008 9:28 pm

Meleagros' problem

Postby rmcavalcante » Wed Sep 17, 2008 1:13 pm

Meleagros,


I understood your problem, and yes, I know about the callback type restriction. I just suggested that because I assumed you need only to display messages, so String callback would be enough.
I don't know if that will help, but have you consider creating a server inside your service? This server could accept notifiers and notify all registered object for every message.
If you take this approach, you'll have to instantiate your Jabber connection inside your server, that is triggered by you Android Service. Your activity will have to start a client for your server, this client will registrate your Activity as a notifier object. For instance, your Activity could implement a custom Notify interface.
So the server will notify your client and your client will notify your Activity.
This approach, IMHO, is good because it detaches your implementation from Android Code. If you implement this Jabber connection inside an Android Service you may have to perform any adaptation whenever Google dicides to refactory their API.
I had a problem like your and that was how we solve it. If you find a better solution, please tell us.

Roberto M. Cavalcante
rmcavalcante
Freshman
Freshman
 
Posts: 7
Joined: Thu Sep 11, 2008 10:48 am

Postby Meleagros » Wed Sep 17, 2008 1:38 pm

is it the observer pattern? is it the same pattern with the service and the callbacks?

with this i can notify my activities with the connection? so i am not restricted with datatypes?
Meleagros
Freshman
Freshman
 
Posts: 7
Joined: Tue Sep 02, 2008 9:28 pm

Postby rmcavalcante » Wed Sep 17, 2008 1:53 pm

Meleagros wrote:is it the observer pattern? is it the same pattern with the service and the callbacks?

with this i can notify my activities with the connection? so i am not restricted with datatypes?


It is the observer pattern.

But it is been performed between your server and the client. Callbacks do impose the type restriction. But you can create your own server attached to Android's Service life cycle. So between your Server and your Client anything goes.
Your client will connect to your server and exchange whatever you want. You may accomplish what you want perhaps by exchanging Objects because they are serializeable and can be converted to the correct type in each peer.
The major drawback is that you'll have to make the type available for both peers, server and client. You will need two Android Services for this: One for your Server and another for your Client, which actually will have an Activity associated to present your Jabber message, so I presume.

Hope it helps
rmcavalcante
Freshman
Freshman
 
Posts: 7
Joined: Thu Sep 11, 2008 10:48 am

Top

Postby Meleagros » Sat Sep 27, 2008 11:30 pm

sooo, sorry for the delay. i solved my problem. it was my stupidity.

my problem was that i dont understand the right usage of the services. i took that the remote service is the right choise because my activitys cannot be blocked by the service.

but i did not understand that the remote service runs in its own process and there is no alternative for the data exchange without aidl.

to solve my problem i delted the remote tag from the android manifest and it works well :!:

the disatvantage is that the service can now block my activitys if there are complex algorithmn or something like that (i think so...)

i implemented now java listener (like you) but i need only one service not two. its the same implementation like the one with aidl.

so for all with the same problem think about the usage of a local service...
---
i forgot: thank you for your suggestion!
Meleagros
Freshman
Freshman
 
Posts: 7
Joined: Tue Sep 02, 2008 9:28 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 8 guests