Understanding how is this interface implemented in AsyncTask [duplicate]

前提是你 提交于 2020-05-09 17:15:37

问题


I wrote a simple weather app which goes fetches data from Openweathermap's API in Json format. I used an Aynctask to fetch the data, using ideas from online (as we all do!)
All works ok, but I am trying to get a better understanding of the callback implementation (like this one one at How to use callback or interface - Pass data from Asynctask to activity
In particular, I am trying to understand how is the interface is implemented without the keyword implements anywhere?

The Async thread:

class MyTask extends AsyncTask {
    public interface OnUpdateListener {
        public void onUpdate(MyObject obj);
    }
    OnUpdateListener listener;
    MyTask() {}
    public void setUpdateListener(OnUpdateListener listener) {
        this.listener = listener;
    }
    MyObject doInBackground() {
        return obj;
    }
    onPostExecute(MyObject obj) {
        if (listener != null) {
            listener.onUpdate(obj);
        }
    }
}

In Mainactivity:

MyActivity extends Activity {
    void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyTask  task = new MyTask();
        task. setUpdateListener(new MyTask. OnUpdateListener() {
            public void onUpdate(MyObject obj) {
                .....
            }
        });
        task.execute();
    }
}

来源:https://stackoverflow.com/questions/61329291/understanding-how-is-this-interface-implemented-in-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!