Guest User

Untitled

a guest
Mar 19th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. /*
  2. * The Code inside the activity. It uses the binder Interface to connect to a running service
  3. * and gets the current contexts of it.
  4. *
  5. * Note: The log output prints out "Received 0 bytes" every time.
  6. */
  7.  
  8. private void updateContexts() throws RemoteException {
  9.         Parcel data = Parcel.obtain();
  10.         Parcel reply = Parcel.obtain();
  11.         binder.transact(CRSBinder.GET_CONTEXTS, data, reply, 0);
  12.        
  13.         Log.i("CRS", "Received " + reply.dataSize() + " bytes");
  14.        
  15.         Parcelable parcelData[] = reply.readParcelableArray(context.Context.class.getClassLoader());
  16. }
  17.  
  18. /*
  19. * The onTransact handler inside the binder. It just delegates the request to the according function
  20. */
  21. @Override
  22. protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
  23. {
  24.     switch(code)
  25.     {
  26.         case NEW_LOCATION: return newContext(data,reply);
  27.         case DELETE_LOCATION: return deleteContext(data, reply);
  28.         case GET_CONTEXTS: return getContexts(data,reply);
  29.         default: throw new IllegalArgumentException();
  30.     }  
  31. }
  32.  
  33. /*
  34. * The actual respond code. The contexts are collected, and sent back using the reply parcel.
  35. *
  36. * Note: The log output prints "386 bytes written to reply"
  37. */
  38. private boolean getContexts(Parcel data, Parcel reply) {
  39.        
  40.     Log.i("CRS", "Replying all contexts");
  41.     List<Context> contexts = service.getContexts();
  42.        
  43.     if(contexts.isEmpty()==false)
  44.     {
  45.         Context contextArray[] = new Context[contexts.size()];
  46.         for(int i=0; i<contexts.size();++i)
  47.         {
  48.             contextArray[i]=contexts.get(i);
  49.         }
  50.        
  51.         reply.writeParcelableArray(contextArray, 0);
  52.         Log.i("CRS", reply.dataSize() + " bytes written to reply");
  53.     }
  54.     return true;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment