title: Passing Custom Binder between packages
body: <p>How do you send a custom Binder object between android packages? say from a service to an activity (in separate packages)?</p>
  
  <p>I've been playing with this for a while but cannot seem to get it to work.</p>
  
  <p>CustomBinder is contained in a library that both packages have a copy of.</p>
  
  <p>The error is at the casting point in onServiceConnected. I always see "Casting Failed".</p>
  
  <p>I've noticed a bit more interesting things to this. When I override the toString method in my CustomBinder, and make it return some string, and I then print out the Binder i get, I get "android.os.BinderProxy" instead.</p>
  
  <p>Updated code to reflect:</p>
  
  <p>Package 1 (Contains the activity)</p>
  
  <pre><code>private ServiceConnection mConnection = new ServiceConnection(){
       @Override
       public void onServiceConnected(ComponentName name, IBinder binder){
            Log.d("TAG",binder.toString());
            try{
                 CustomBinder nBinder = (CustomBinder)binder;
                 Log.d("TAG","It worked!");
            }catch(Exception e){
                 Log.d("TAG","Casting failed again");
            }
       }
  
       @Override
       public void onServiceDisconnect(ComponentName name){}
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       Intent intent = new Intent();
       intent.setComponent(new ComponentName("com.example.testService","com.example.testService.MainService");     
       bindService(intent, mConnection, BIND_AUTO_CREATE);
  }
  </code></pre>
  
  <p>Package 2 (Contains the Service)</p>
  
  <p>...</p>
  
  <pre><code>@Override
  public onBind(Intent intent){
       CustomBinder binder = new CustomBinder();
       return binder;
  }
  </code></pre>
  
  <p>CustomBinder in Library</p>
  
  <pre><code>// This literally does nothing for now
  Public class CustomBinder extends Binder{
       @Override
       public String toString(){
            return "CustomBinder";
       }
  }
  </code></pre>