JamClerk
JamClerk

Reputation: 83

android bind service to activity

I have seen several similar examples here but can't seem to get my service to bind with activity.

I am getting the error

"android.os.binderproxy cannot be cast to IC_CommissaryService".

My service looks like this:

public class IC_CommissaryService extends Service 
{
    @Override
    public IBinder onBind(Intent intent) 
    {
        return mBinder;
    }

    private final IBinder mBinder = new LocalBinder();          

    public class LocalBinder extends Binder 
    {       
        IC_CommissaryService getService() 
        {
            return IC_CommissaryService.this;
        }
    }

    public int onStartCommand(Intent intent, int flags, int startId)
    {
    }

    private boolean SendOrderToServer(int orderID)
    {
        /* do stuff*/
    }

}

and my activity looks like this:

public class SubmitOrders extends Activity 
{
    IC_CommissaryService ICservice;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        Intent serviceintent = new Intent(this, IC_CommissaryService.class);
        serviceintent.putExtra("binded", true);
        bindService(serviceintent, mConnection, Context.BIND_AUTO_CREATE);
    }

    private ServiceConnection mConnection = new ServiceConnection() 
    {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) 
        {
            Log.e("TEST", "SERVICE CONNECTED");
            try
            {
                ICservice =(IC_CommissaryService.LocalBinder)service).getService();
                for(int i = 0; i < Submitorders.size(); i++)
                {
                    ICservice.SendOrderToServer(Submitorders.get(i).intValue());                                                
                }
            }
            catch(Exception ex)
            {
                Log.e("Error", "Error connecting service: " + ex.getMessage());
            } 
        }

        @Override
        public void onServiceDisconnected(ComponentName className) 
        {           
        }
    };
}

I am getting the error in my activity on the line ICservice =(IC_CommissaryService.LocalBinder)service).getService();

I think I have done the same as people already suggested in other posts so any help please?

thanks

Upvotes: 4

Views: 3478

Answers (3)

frenchie4111
frenchie4111

Reputation: 399

These are the abstract classes that I use to solve this problem: https://gist.github.com/frenchie4111/6086c6e4327d7936364a

Just extend both these classes with your service and activity (You can change the fragment from a fragment to an activity with ease). And make sure that in your service/fragment onCreate you set the serviceClass like so:

public void onCreate( Bundle savedInstance ) {
    super.onCreate( savedInstance );
    this.serviceClass = IC_CommissaryService.class;
}

Upvotes: 0

UXkQEZ7
UXkQEZ7

Reputation: 1184

Quote from the Bound Services documentation:

If your service is private to your own application and runs in the same process as the client (which is common), you should create your interface by extending the Binder class and returning an instance of it from onBind().

Remove the android:process attribute in in AndroidManifest.ml to make the service run in the same process. I had the same problem today it did the trick.

Upvotes: 0

android learner
android learner

Reputation: 29

I had the same kind of problem. I just figured it out today. Please look at the parts annotated with <<===== below. I hope it helps.

public class PracticeServiceBindingActivity extends ListActivity {
       private MyService.MyBinder service;   <<====
       ....

       private ServiceConnection connection = new ServiceConnection( ){

               public void onServiceConnected (ComponentName name, IBinder service) {
                      setService(MyService.MyBinder) service;  <<====
               ....
               }
       }

       public void onCreate(....) {

               ...
               public MyService.MyBinder getService(){     <<=====
                      return service;
               }
               public void setService(MyService.MyBinder service) {   <<=====
                      this.service = service;
               }
       }

}

Upvotes: 1

Related Questions