Android Skip custom object from service to action

I am creating Instant Messenger for android using asmack. I started a chat service that connects to the xmpp server. the service connects to the xmpp server and I get a list and presence. but now I need to update the interface and pass the list of account objects from service to activity. I came across Parcelable and serializable. I cannot figure out what is the right way for this service. Can someone provide some code examples where I can do the same.

thank

+3
source share
1 answer

. smack, , Activity. AIDL . AIDL . . !

.aidl, , . AIDL - . , 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();

}

, ADT ObjectFromService2Activity gen/.

Android SDK (command line) compiler aidl ( tools/), Java, Eclipse.

obBind() . , 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()");
 }
}

, , ServiceConnection. ,

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;
    }
};

ObjectFromService2Activity, s1.getObjectFromService() JSONObject. Fun!

+1

All Articles