6

I am creating an Instant Messenger for android using asmack. I have started a Chat service which connects to xmpp server. the service connects to xmpp server and i am getting the roster and presence. but now i have to update the UI and pass the list of account objects from service to activity. i have come across Parcelable and serializable. i can not figure out what is the correct way for this service. Can some one provide some code samples where i can do the same.

Thanks

navraj
  • 95
  • 1
  • 8
  • 1
    http://stackoverflow.com/questions/3747448/android-passing-data-between-service-and-activity or http://stackoverflow.com/questions/9239240/passing-object-through-intent-from-background-service-to-an-activity – owen gerig May 23 '12 at 14:07

1 Answers1

1

you're making a nice app. I don't know more about smack but I know how to pass objects from service to Activity. You can make AIDL for your service. AIDL'll pass your service objects to activity. And then you can update your Activity UI. This link might be helpful to you!

First you have to make .aidl file using your editor and save this file on your desktop. AIDL is just like an interface nothing else. Like, ObjectFromService2Activity.aidl

package com.yourproject.something

// Declare the interface.
interface ObjectFromService2Activity {
    // specify your methods 
    // which return type is object [whatever you want JSONObject]
    JSONObject getObjectFromService();

}

Now copy this file and paste it to your project folder and the ADT plugin will generate the ObjectFromService2Activity interface and stub automatically in gen/ folder.

The Android SDK also includes a (command line) compiler aidl (in the tools/ directory) that you can use to generate the java code in case you don't use Eclipse.

Override obBind() method in your service. Like, Service1.java

public class Service1 extends Service {
private JSONObject jsonObject;

@Override
public void onCreate() {
  super.onCreate();
  Log.d(TAG, "onCreate()");
  jsonObject = new JSONObject();
}

@Override
public IBinder onBind(Intent intent) {

return new ObjectFromService2Activity.Stub() {
  /**
   * Implementation of the getObjectFromService() method
   */
  public JSONObject getObjectFromService(){
    //return your_object;
    return jsonObject;
  }
 };
}
@Override
public void onDestroy() {
   super.onDestroy();
   Log.d(TAG, "onDestroy()");
 }
}

Start your service using your activity or where you want to start this service and make ServiceConnection. Like,

Service1 s1;
private ServiceConnection mConnection = new ServiceConnection() {
    // Called when the connection with the service is established
    public void onServiceConnected(ComponentName className, IBinder service) {
        // Following the example above for an AIDL interface,
        // this gets an instance of the IRemoteInterface, which we can use to call on the service
        s1 = ObjectFromService2Activity.Stub.asInterface(service);
    }

    // Called when the connection with the service disconnects unexpectedly
    public void onServiceDisconnected(ComponentName className) {
        Log.e(TAG, "Service has unexpectedly disconnected");
        s1 = null;
    }
};

Using object of ObjectFromService2Activity you can access method s1.getObjectFromService() will return JSONObject. More Help Fun!

vajapravin
  • 1,363
  • 3
  • 16
  • 29
  • I also looked at the broadcastreciever as it is capable of passing objects through Intent by serializable extra. Is there some way i can pass object to the activity with this method? also do you have some good example of aidl? – navraj May 24 '12 at 06:09
  • When you need to perform IPC, using a Messenger for your interface is simpler than implementing it with AIDL, because Messenger queues all calls to the service, whereas, a pure AIDL interface sends simultaneous requests to the service, which must then handle multi-threading. For most applications, the service doesn't need to perform multi-threading, so using a Messenger allows the service to handle one call at a time. If it's important that your service be multi-threaded, then you should use AIDL to define your interface. – Mahesh Feb 24 '16 at 14:30