Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * The Code inside the activity. It uses the binder Interface to connect to a running service
- * and gets the current contexts of it.
- *
- * Note: The log output prints out "Received 0 bytes" every time.
- */
- private void updateContexts() throws RemoteException {
- Parcel data = Parcel.obtain();
- Parcel reply = Parcel.obtain();
- binder.transact(CRSBinder.GET_CONTEXTS, data, reply, 0);
- Log.i("CRS", "Received " + reply.dataSize() + " bytes");
- Parcelable parcelData[] = reply.readParcelableArray(context.Context.class.getClassLoader());
- }
- /*
- * The onTransact handler inside the binder. It just delegates the request to the according function
- */
- @Override
- protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
- {
- switch(code)
- {
- case NEW_LOCATION: return newContext(data,reply);
- case DELETE_LOCATION: return deleteContext(data, reply);
- case GET_CONTEXTS: return getContexts(data,reply);
- default: throw new IllegalArgumentException();
- }
- }
- /*
- * The actual respond code. The contexts are collected, and sent back using the reply parcel.
- *
- * Note: The log output prints "386 bytes written to reply"
- */
- private boolean getContexts(Parcel data, Parcel reply) {
- Log.i("CRS", "Replying all contexts");
- List<Context> contexts = service.getContexts();
- if(contexts.isEmpty()==false)
- {
- Context contextArray[] = new Context[contexts.size()];
- for(int i=0; i<contexts.size();++i)
- {
- contextArray[i]=contexts.get(i);
- }
- reply.writeParcelableArray(contextArray, 0);
- Log.i("CRS", reply.dataSize() + " bytes written to reply");
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment