在接口实现中获取自我实例

时间:2013-05-10 01:03:14

标签: java android

我是java / android的新手。

我有一个服务类,它有2个方法.... prepare()和execute()..

两者都是异步调用,所以我要做的是,调用prepare()并等待prepareFinished我正在听...当prepare方法完成时,对于同一个服务实例我想要调用execute()方法.....

按照下面的尝试:

for(int idx = 0; idx < services.length; idx++)
{
    MyService instance = services[idx];
    instance.setDataReadListener(new AsyncDataReadListener() {
        @Override
        public void prepareFinished(ServiceInfo info) {

           //I would like to get the self instannce here to call another 
                       //method after been prepared. something like::: sender.execute()

        }
    });
}

//In a button click I call above,  for each service its prepared I would like another method of the same service instance to be called:
for(int idx = 0; idx < services.length; idx++)
{
    MyService instance = services[idx];
    //async call
    instance.prepare();
}

我怎样才能完成它?

韩国社交协会

3 个答案:

答案 0 :(得分:0)

制作实例最终版:

final MyService instance = services[idx];

您可以在instance.execute()内拨打prepareFinished

答案 1 :(得分:0)

制作instance final并直接引用它:

for(int idx = 0; idx < services.length; idx++)
{
    final MyService instance = services[idx];
    instance.setDataReadListener(new AsyncDataReadListener() {
        @Override
        public void prepareFinished(ServiceInfo info) {
            instance.execute()
        }
    });
}

答案 2 :(得分:0)

这种方法有什么问题?

final MyService instance = services[idx];
instance.setDataReadListener(new AsyncDataReadListener() {
    @Override
    public void prepareFinished(ServiceInfo info) {

       instance.execute();
    }
});