简体   繁体   English

绑定到服务-绑定成功后回调-Android

[英]Bind to a service - call back when bind has been successfully - Android

I've come to a point where I don't know an elegant way to do this. 我到了一个我不知道如何做到这一点的优雅的地步。

Let's say I've a Fragment , named FragmentA , and a Service named BackupService 比方说,我有一个Fragment ,命名FragmentAService命名BackupService

On FragmentA I bound it to the BackupService using: FragmentA我使用以下命令将其绑定到BackupService

private ServiceConnection backupServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        backupBoundService = binder.getService();
        isBound = true;
        // How to let the fragment know this has happened?
        // Use an eventBus? EventBus.getDefault().post(backupBoundService); ?
        Log.d(TAG, "On Service Connected -> Yup!");
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        isBound = false;
    }
};

And: 和:

Intent intent = new Intent(ApplicationContextProvider.getContext(), BackupsService.class);
ApplicationContextProvider.getContext().bindService(intent, backupServiceConnection, Context.BIND_AUTO_CREATE); // Using application context

Now I know the binding is an asynchronous task and here is where my question comes in. 现在我知道绑定是一个asynchronous任务,这是我的问题所在。

I came up with the idea of using an EventBus but I don't find it elegant as the fragment would be posting the object (in this case backupBoundService ), referencing the service, and at the same time would be listening/receiving the event from the bus, eg, would be the same fragment posting and receiving the event (posting to himself). 我想到了使用EventBus的想法,但是我觉得它并不优雅,因为该片段将发布对象(在本例中为backupBoundService ),引用服务,同时将监听/接收事件。总线,例如,将是相同的片段发布和接收事件(发布给自己)。

Is there an elegant way to get a reference for the running service when the fragment is bounded to it? 当片段绑定到正在运行的服务时,是否有一种优雅的方式来获取引用? I'm quite sure there's a pattern for this case but I've been googling and searching here to no luck so far. 我很确定这种情况有一种模式,但到目前为止,我一直在谷歌上搜索并没有出现任何运气。

Hi you can use eventbus or you can create simple call back methods using Interfcae for you requirement. 嗨,您可以使用事件总线,也可以根据需要使用Interfcae创建简单的回调方法。

If you don't have idea to create call backs using interface then look this piece of code. 如果您不打算使用接口创建回调,请查看这段代码。 It is same like eventbus :) 就像eventbus一样:)

  // The callback interface
  interface MyCallback {
  void callbackCall();
  }

 // The class that takes the callback
 class Worker {
 MyCallback callback;

  void onEvent() {
  callback.callbackCall();
  }
  public void setCallBack(MyCallback callback)
     this.callback=callback;
  }
 /////////////////////////////
class Callback implements MyCallback {

  ....
 Worker worker= new Worker()
 Worker.setCallback(this);      
 .. .

  void callbackCall() {
  // callback code goes here
  //onEvent this method will execute
 }

}   

Hope this will helpful. 希望这会有所帮助。 Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM