tags:

views:

196

answers:

2

Hello all,I have made an application based on the android API's remote service application wich uses callbacks to notify the main activity for changes, the app works fine passing just a single int value from the remote service back to the activity. The problem is that I would like to pass some Strings and not an int back in order to update the activiti's UI, how can I do that? I have thought of returning an object but there is lack of documentation and don't know how or if it is possible. thanks in advanced maxsap.

A: 

You can pass Strings by default and you should take a look to Parcelable objects (there is no lack of documentation)

fedj
How can I pass strings can you point my to a tutorial or example?the documentation didnt work for me, In the sdk examples in the RemoteServiceBinding you implement the method that defines the callback interface the code is:public voidvalueChanged(intvalue){mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG,value, 0));}wich passes the returned int value to a Message object and then simply updates the ui inside the handler, I have changes the method inside the interface and now accepts strings,but I cant update the UI from ValueChanged method and I cant pass strings to Message object.
maxsap
http://developer.android.com/guide/developing/tools/aidl.html
fedj
I am aware of that article it just describes what is done on the RemoteService example didn't help me much as it only shows how to pass tha arg back and just says about parcables not showing how to do it!
maxsap
You have an example with Rect ...
fedj
yes I know it but for some reason I am stuck! I have a class that implements Parcable just like in the rect example,I have the aidl interface,from the service class I do: ParcableInfo parceInfo = new ParcableInfo(ID);mCallbacks.getBroadcastItem(i).valueChanged(parceInfo);and then from the main activity I do:CellInfo cellInfo = new CellInfo(null, null, null, null, null, null, null);cellInfo.setDeviceId(info.ID);Message msg = mHandler.obtainMessage(BUMP_MSG);msg.obj = cellInfo;mHandler.sendMessage(msg);This is simply wrong right?what I am doing wrong?
maxsap
A: 

Ok just figure it out, I am posting the solution in order to help others with similar problem.

1.you need to make a separate aidl file that only contains the parcable object(see the link in the comments above).

  1. Second in the callback interface import that interface and then add it as a parameter.
  2. when you implement the method from the callback interface in your activity class add the returned object (this would be the parsable object) as the Message object and send that message.
  3. in the handler just unmarshall the object and use it.

This is a sample code:

The Parcable's object interface:

    package sap.max;
    parcelable ParcableInfo;

The Callback interface:

package sap.max;
import sap.max.ParcableInfo;
/**
* A callback interface used by IMonitorService to send
* synchronous notifications back to its clients.  Note that this is a
* one-way interface so the server does not block waiting for the client.
*/
oneway interface IRemoteServiceCallback {
/** 
 * Called when the service has a new value for you.
 */
void valueChanged(in ParcableInfo info);
}

The service's actions (note this is only what the service will call not complete code):

private final Handler mHandler = new Handler() {
    @Override public void handleMessage(Message msg) {
        switch (msg.what) {

            // It is time to bump the value!
            case REPORT_MSG: {
                // Up it goes.
                int value = ++mValue;

                // Broadcast to all clients the new value.
                final int N = mCallbacks.beginBroadcast();
                for (int i=0; i<N; i++) {
                    try {
                        ParcableInfo parceInfo = new               ParcableInfo(telephonyManager.getSubscriberId() );
                        parceInfo.setID(telephonyManager.getSubscriberId());
                        mCallbacks.getBroadcastItem(i).valueChanged(parceInfo);                      
                    } catch (RemoteException e) {
                        // The RemoteCallbackList will take care of removing
                        // the dead object for us.
                    }
                }
                mCallbacks.finishBroadcast();

                // Repeat every 3 second.
                sendMessageDelayed(obtainMessage(REPORT_MSG), 1*3000);
            } break;
            default:
                super.handleMessage(msg);
        }
    }
};

And finally the Activities part:

private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
            @Override
    public void valueChanged(ParcableInfo info) throws RemoteException {
        CellInfo cellInfo = new CellInfo(null, null, null, null, null, null, null);
        cellInfo.setDeviceId(info.ID);
        Message msg = mHandler.obtainMessage(BUMP_MSG, info);   
        mHandler.sendMessage(msg);
    }

And the message is handled by:

//this is the handler for the service...
private Handler mHandler = new Handler() {
    @Override public void handleMessage(Message msg) {
        switch (msg.what) {
            case BUMP_MSG:
                ParcableInfo info = (ParcableInfo) msg.obj;
                configView.setText(info.ID);
                Toast.makeText(AntiTheft.this, "Received from service: " + info.ID,
                        Toast.LENGTH_SHORT).show();  
                break;
            default:
                super.handleMessage(msg);
        }
    }

};

Hope this helps others, thanks for the help fedj. regards maxsap.

maxsap