How do I bind this service in Android?

前端 未结 3 723
小鲜肉
小鲜肉 2020-12-06 02:36

This is the code in my Activity. Initiate an Intent, then a Connection, right?

hello_service = new Intent(this         


        
3条回答
  •  感动是毒
    2020-12-06 02:58

    To connect a service to an activity, all you need to write in a ServiceConnection implementation is :

    @Override
    public void onServiceDisconnected(ComponentName name) {
    mServiceBound = false;
    }
    
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    MyBinder myBinder = (MyBinder) service;
    mBoundService = myBinder.getService();
    mServiceBound = true;
    }
    

    Here mBoundService is an object of your bound service. Have a look at this Bound Service Example.

提交回复
热议问题