How to communicate and exchange objects with a Local Service

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

How to communicate and exchange objects with a Local Service

Postby zoniq » Fri Oct 09, 2009 9:42 am

So far I've a class for a simple local service. What I want to do is to send requests to this local service from different activies in my application. Depending on the parameters of these requests the service will connect via HttpClient to a webserver and receive a JSONObject and return it to the activity. All the HTTP communication is already working within my activity, but I'd like it to run in a separate thread in my local service now.

The source code of my very simple local service looks like this so far:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // BackgroundService.java
  2. package com.test.localservice;
  3.  
  4. import android.app.Service;
  5. import android.content.Intent;
  6. import android.os.IBinder;
  7. import android.util.Log;
  8. public class BackgroundService extends Service
  9. {
  10.     @Override
  11.     public void onCreate() {
  12.         super.onCreate();
  13.         Log.i("BackgroundService", "onCreate()");
  14.         Thread thr = new Thread(null, new RunThread(), "BackgroundService");
  15.         thr.start();
  16.     }
  17.     class RunThread implements Runnable
  18.     {
  19.         public void run() {
  20.             Log.i("BackgroundService", "run()");
  21.  
  22.             /* Here the HTTP JSON communication is going to happen */
  23.  
  24.             //BackgroundService.this.stopSelf();
  25.         }
  26.     }
  27.     @Override
  28.     public void onDestroy()
  29.     {
  30.         Log.i("BackgroundService", "onDestroy()");
  31.         super.onDestroy();
  32.     }
  33.  
  34.     @Override
  35.     public void onStart(Intent intent, int startId) {
  36.         Log.i("BackgroundService", "onStart()");
  37.         super.onStart(intent, startId);
  38.     }
  39.     @Override
  40.     public IBinder onBind(Intent intent) {
  41.         Log.i("BackgroundService", "onBind()");
  42.         return null;
  43.     }
  44.  
  45. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


The problem I'm facing now (due to a lack of knowledge) is the communication part Activities <--> Local Service. I found some communication and object exchange examples for a Remote Service using AIDL (Android Interface Definiton Language). However, I'm not sure if I've to go this path. I would simply like to exchange either my own defined objects or if that is not possible then just a JSONObject or if it makes things much easier even just simple String Arrays would work (for now).

Can someone please point me in the right direction and if possible give a simple example of an Object exchange (both ways) with a local service.

Thanks in advance.
zoniq
Junior Developer
Junior Developer
 
Posts: 21
Joined: Mon Sep 28, 2009 12:33 pm

Top

Postby padde » Fri Oct 09, 2009 11:08 am

There is an example in the APIDemos shipped with the SDK.

Its under .../<sdk>/platforms/<sdk>/samples/ApiDemos/src/com/example/android/apis/app

Nice path eh? :twisted:

Relevant files are LocalService.java, LocalServiceBinding.java and LocalServiceController.java
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby zoniq » Fri Oct 09, 2009 11:13 am

I've already looked at that example, as well as the Remote Service example. And my code above is pretty much created from that. However, all the Local Service example does is starting and stopping a service. What I would like to know though, is how to communicate and exchange objects with my activies.

Any thoughts on that?
zoniq
Junior Developer
Junior Developer
 
Posts: 21
Joined: Mon Sep 28, 2009 12:33 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 23 guests