java - 安卓 : How to get callback from custom method?

标签 java android

我有以下类(class):

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;
    }

}

我可以使用以下方法从 Controller 获取实例:

mTimerTaskHelper = TimerTaskHelper.getInstance(container);

但我想在每个之后在 Controller 中获得回调

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

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

来自 Controller 。

请问我怎样才能以最好的方式做到这一点?

最佳答案

我想你想要这样的东西。

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
  }});

关于java - 安卓 : How to get callback from custom method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33234696/

相关文章:

android - "noinspection"Android Studio 关键字列表

java - 我需要将 void 输出转换为字符串,以便我可以比较两个字符串

java - 将 XML 文件转换为单个字符串时出现问题

java - 奇怪的 Java for 循环格式

android - 在android中滑动图像

java - 如何将项目添加到文本选择弹出菜单?

Java - 查找最小数

java - XMLHTTP 请求错误。带有 tomee 服务器的 Eclipse 项目。

android - 将对象添加到数组对象在 android 中不起作用

android - 在 Android 上如何将对象从一个 Activity 传递到另一个 Activity?