Binder课程做什么?Binding是什么意思?在Android有界服务?

Moh*_*dhi 7 android android-service android-binder

我完全与Bounded Services混淆.我的问题是: - Binding是什么意思?Binder课程做什么?从服务返回iBinder对象是什么意思?什么是IBinder对象?onBind方法的工作原理是什么?这些是关于有界服务的几个问题.请详细解释我.我已经阅读了这些文件.目前还不清楚.

Rev*_*ppa 8

绑定服务:

绑定服务是允许应用程序组件通过调用bindService()来创建长期连接来绑定它的服务.

如果要从应用程序中的活动和其他组件与服务交互,或者通过进程间通信(IPC)将某些应用程序的功能公开给其他应用程序,请创建绑定服务.

要创建绑定服务,请实现onBind()回调方法以返回定义用于与服务进行通信的接口的IBinder.然后,其他应用程序组件可以调用bindService()来检索接口并开始调用服务上的方法.该服务仅用于服务绑定到它的应用程序组件,因此当没有绑定到该服务的组件时,系统会销毁它.您不需要以通过onStartCommand()启动服务时必须的方式停止绑定服务.

的IBinder:

要创建绑定服务,必须定义指定客户端如何与服务通信的接口.服务和客户端之间的这个接口必须是IBinder的实现,并且是您的服务必须从onBind()回调方法返回的.客户端收到IBinder后,可以通过该接口开始与服务进行交互.

onBind():

当另一个组件想要与服务绑定(例如执行RPC)时,系统通过调用bindService()来调用此方法.在此方法的实现中,您必须提供一个客户端用于通过返回IBinder与服务进行通信的接口.您必须始终实现此方法; 但是,如果您不想允许绑定,则应返回null.


Ayo*_*rog 5

这是一个例子,作为上述答案的补充

  1. 在您的服务类中,使用我们内部类创建的对象初始化IBinder接口(检查步骤 2)
  2. 创建一个内部类扩展具有 getter 函数的Binder,以获得对服务类的访问
  3. 在您的服务类ovveride onBind函数中,并使用它来返回我们在步骤 1 中创建的实例

**代码将为您清除它**

public class MyServiceClass extends Service {

//binder given to client  
private final IBinder mBinder = new LocalBinder();

//our inner class 
public LocalBinder extends Binder {
public MyServiceClass getService() {
        return MyServiceClass.this;
    }
}
@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public void doSomeWork(int time){ //we will call this function from outside, which is the whole idea of this **Binding**}

  }
Run Code Online (Sandbox Code Playgroud)

下一步是绑定自己

在你的 MainClass 或任何你想绑定你的服务的地方,

  1. 定义服务绑定的回调,传递给 bindService()

    私人服务连接服务连接=新服务连接(){

    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
       MyServiceClass.LocalBinder binder =(MyServiceClass.LocalBinder)service;
       timerService = binder.getService();
    }
    
    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        //what to do if service diconnect
    }
    
    Run Code Online (Sandbox Code Playgroud)

    };

  2. 绑定的那一刻

    public class MyServiceClass extends Service {
    
    //binder given to client  
    private final IBinder mBinder = new LocalBinder();
    
    //our inner class 
    public LocalBinder extends Binder {
    public MyServiceClass getService() {
            return MyServiceClass.this;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    
    public void doSomeWork(int time){ //we will call this function from outside, which is the whole idea of this **Binding**}
    
      }
    
    Run Code Online (Sandbox Code Playgroud)

解绑服务

unbindService(serviceConnection);
Run Code Online (Sandbox Code Playgroud)
  1. 然后你使用 [timerService = binder.getService();] 的帮助调用我们之前在 Service 类中创建的公共函数
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
       MyServiceClass.LocalBinder binder =(MyServiceClass.LocalBinder)service;
       timerService = binder.getService();
    }
    
    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        //what to do if service diconnect
    }
    
    Run Code Online (Sandbox Code Playgroud)