Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取接口实现内部的自实例_Java_Android - Fatal编程技术网

Java 获取接口实现内部的自实例

Java 获取接口实现内部的自实例,java,android,Java,Android,我是java/android新手 我有一个服务类,它有两个方法。。。。准备并执行 这两个都是异步调用,所以我要做的是,调用prepare并等待prepareFinished,我正在听。。。。当prepare方法完成时,对于同一个服务实例,我想调用execute方法 请按照我下面的尝试: for(int idx = 0; idx < services.length; idx++) { MyService instance = services[idx]; instance.s

我是java/android新手

我有一个服务类,它有两个方法。。。。准备并执行

这两个都是异步调用,所以我要做的是,调用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();
}
我怎样才能做到呢

Tks.

使实例成为最终实例:

final MyService instance = services[idx];
您可以在prepareFinished内调用instance.execute。

将实例设为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()
        }
    });
}

这种方法有什么问题

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

       instance.execute();
    }
});