Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Pass Custom Object from Service to activity

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

like image 629
navraj Avatar asked May 23 '12 13:05

navraj


People also ask

How can I pass value from one activity to another activity in Android without intent?

This example demonstrate about How to send data from one activity to another in Android without intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How pass data from one activity to another activity in Android?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.


1 Answers

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!

like image 111
vajapravin Avatar answered Sep 20 '22 00:09

vajapravin