【问题标题】:Callback on library android库android上的回调
【发布时间】:2017-03-09 02:46:32
【问题描述】:

我正在尝试使用事件总线将事件从我的库通知到服务。 到目前为止,我一直在使用这样的 EventBus:

public abstract class MLibraryClass{

....
EventBus.getDefault().post(event);
...

}

public class MService{
...
public void onEvent(Event event){
 //do stuff
}

}

现在我创建了一个接口来使用回调。问题是回调有一个空引用,我不知道如何实例化它。

界面:

public Interface MInterface{
    void doStuff(Event event);
}

库类:

public abstract class MLibraryClass{
...
MInterface interface;
Event event;
...
public void onEvent(){
    interface.doStuff(event)
}
...

}

服务:

public class MService implements MInterface{
...
void doStuff(Event event){
    //some code
}
...

}

【问题讨论】:

    标签: android service interface callback


    【解决方案1】:

    您需要在您的MLibraryClass 中有一个方法,如下所示并执行以下操作:

    public abstract class MLibraryClass{
    ...
    MInterface interface;
    Event event;
    ...
    public void onEvent(){
        if(interface!=null) interface.doStuff(event)
    }
    
    public void setInterace(MyInterface myInterface){
        this.interface = myInterface;
    }
    ...
    

    Android 从你访问 MLibraryClass 的地方,设置接口。

    【讨论】:

      猜你喜欢
      • 2015-07-21
      • 1970-01-01
      • 2021-09-18
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多