Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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 Android:如何从自定义方法获取回调?_Java_Android - Fatal编程技术网

Java Android:如何从自定义方法获取回调?

Java Android:如何从自定义方法获取回调?,java,android,Java,Android,我有以下课程: public class TimerTaskHelper { private static TimerTaskHelper instance = null; private static Integer ONE_MINUTE = 60000; private static Handler handler; private static Runnable runnableInstance; private static Container

我有以下课程:

public class TimerTaskHelper {

    private static TimerTaskHelper instance = null;
    private static Integer ONE_MINUTE = 60000;
    private static Handler handler;
    private static Runnable runnableInstance;
    private static Container container;

    public static TimerTaskHelper getInstance(Container c) {
        if (instance == null) {
            instance = new TimerTaskHelper(c);
        }
        return instance;
    }

    public TimerTaskHelper(Container c) {
        this.handler =  new Handler();
        this.runnableInstance = runnableCode;
        this.container = c;
        // Kick off the first runnableInstance task right away
        handler.post(runnableCode);
    }

    // Define the task to be run here
    private Runnable runnableCode = new Runnable() {
        @Override
        public void run() {
            // Do something here on the main thread
            Logger.d("Called");
            // Repeat this runnableInstance code again every 2 seconds
            handler.postDelayed(runnableCode, 2000);
            container.notifyObservers();
        }
    };

    public void stopExecution() {
        handler.removeCallbacks(runnableInstance);
        instance = null;
    }

}
我可以使用以下命令从控制器获取实例:

mTimerTaskHelper = TimerTaskHelper.getInstance(container);
但我想在每次测试之后在控制器中得到回调

   private Runnable runnableCode = new Runnable() {
        @Override
        public void run() {

来自控制器


我怎样才能用最好的方式来做呢?

我想你想要这样的东西

interface CallBack {
void callBackMethod();
}

public class TimerTaskHelper {

private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
private CallBack callback;
public void setCallBack(CallBack cb){
 callback=cb;
}
public static TimerTaskHelper getInstance(Container c) {
    if (instance == null) {
        instance = new TimerTaskHelper(c);
    }
    return instance;
}

public TimerTaskHelper(Container c) {
    this.handler =  new Handler();
    this.runnableInstance = runnableCode;
    this.container = c;
    // Kick off the first runnableInstance task right away
    handler.post(runnableCode);
}

// Define the task to be run here
private Runnable runnableCode = new Runnable() {
    @Override
    public void run() {
        // Do something here on the main thread
        Logger.d("Called");
        // Repeat this runnableInstance code again every 2 seconds
        handler.postDelayed(runnableCode, 2000);
        container.notifyObservers();
        if(callback!=null){
           callback.callBackMethod();
    }
};
  mTimerTaskHelper = TimerTaskHelper.getInstance(container);
  mTimerTaskHelper.setCallBack(new CallBack(){
  void callBackMethod(){
  //TODO your code
  }});

我想你想要这样的东西

interface CallBack {
void callBackMethod();
}

public class TimerTaskHelper {

private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
private CallBack callback;
public void setCallBack(CallBack cb){
 callback=cb;
}
public static TimerTaskHelper getInstance(Container c) {
    if (instance == null) {
        instance = new TimerTaskHelper(c);
    }
    return instance;
}

public TimerTaskHelper(Container c) {
    this.handler =  new Handler();
    this.runnableInstance = runnableCode;
    this.container = c;
    // Kick off the first runnableInstance task right away
    handler.post(runnableCode);
}

// Define the task to be run here
private Runnable runnableCode = new Runnable() {
    @Override
    public void run() {
        // Do something here on the main thread
        Logger.d("Called");
        // Repeat this runnableInstance code again every 2 seconds
        handler.postDelayed(runnableCode, 2000);
        container.notifyObservers();
        if(callback!=null){
           callback.callBackMethod();
    }
};
  mTimerTaskHelper = TimerTaskHelper.getInstance(container);
  mTimerTaskHelper.setCallBack(new CallBack(){
  void callBackMethod(){
  //TODO your code
  }});

我想你想要这样的东西

interface CallBack {
void callBackMethod();
}

public class TimerTaskHelper {

private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
private CallBack callback;
public void setCallBack(CallBack cb){
 callback=cb;
}
public static TimerTaskHelper getInstance(Container c) {
    if (instance == null) {
        instance = new TimerTaskHelper(c);
    }
    return instance;
}

public TimerTaskHelper(Container c) {
    this.handler =  new Handler();
    this.runnableInstance = runnableCode;
    this.container = c;
    // Kick off the first runnableInstance task right away
    handler.post(runnableCode);
}

// Define the task to be run here
private Runnable runnableCode = new Runnable() {
    @Override
    public void run() {
        // Do something here on the main thread
        Logger.d("Called");
        // Repeat this runnableInstance code again every 2 seconds
        handler.postDelayed(runnableCode, 2000);
        container.notifyObservers();
        if(callback!=null){
           callback.callBackMethod();
    }
};
  mTimerTaskHelper = TimerTaskHelper.getInstance(container);
  mTimerTaskHelper.setCallBack(new CallBack(){
  void callBackMethod(){
  //TODO your code
  }});

我想你想要这样的东西

interface CallBack {
void callBackMethod();
}

public class TimerTaskHelper {

private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
private CallBack callback;
public void setCallBack(CallBack cb){
 callback=cb;
}
public static TimerTaskHelper getInstance(Container c) {
    if (instance == null) {
        instance = new TimerTaskHelper(c);
    }
    return instance;
}

public TimerTaskHelper(Container c) {
    this.handler =  new Handler();
    this.runnableInstance = runnableCode;
    this.container = c;
    // Kick off the first runnableInstance task right away
    handler.post(runnableCode);
}

// Define the task to be run here
private Runnable runnableCode = new Runnable() {
    @Override
    public void run() {
        // Do something here on the main thread
        Logger.d("Called");
        // Repeat this runnableInstance code again every 2 seconds
        handler.postDelayed(runnableCode, 2000);
        container.notifyObservers();
        if(callback!=null){
           callback.callBackMethod();
    }
};
  mTimerTaskHelper = TimerTaskHelper.getInstance(container);
  mTimerTaskHelper.setCallBack(new CallBack(){
  void callBackMethod(){
  //TODO your code
  }});

第一件事:让你的属性。。。静止的第一件事:让你的属性。。。静止的第一件事:让你的属性。。。静止的第一件事:让你的属性。。。静止的