AsyncTask成功后回调执行调用者的方法

package world.hello.tasksuccall;

import android.os.AsyncTask;
import android.util.Log;

import static android.content.ContentValues.TAG;

//第一个参数是传入的参数
//第二个参数是界面刷新时候用的参数
//第三个参数是返回的参数
public class GetLocalVideosTask extends AsyncTask {

    //刷新界面的部分
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        Log.d(TAG, "onProgressUpdate: "+values+" 这里可以显示界面");
    }


    @Override
    protected String doInBackground(Void... voids) {
        for (int i = 0; i < 1000; i++) {
            //当到500时候刷新界面
            if (i==500)publishProgress(i);
        }
        return "abc.mp4";
    }

    @Override
    protected void onPostExecute(String videoName) {
        super.onPostExecute(videoName);
        this.onSuccessListener.onSuccess(videoName);
    }
    //创建接口,成功时候回调
    private OnSuccessListener onSuccessListener;

    public interface OnSuccessListener {
        void onSuccess(String videoName);
    }

    public void setOnSuccessListener(OnSuccessListener onSuccessListener) {
        this.onSuccessListener = onSuccessListener;
    }
}
package world.hello.tasksuccall;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements GetLocalVideosTask.OnSuccessListener {

    GetLocalVideosTask getLocalVideosTask;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getLocalVideosTask=new GetLocalVideosTask();
        getLocalVideosTask.setOnSuccessListener(this);
        getLocalVideosTask.execute();
    }

//成功时候回调执行
    @Override
    public void onSuccess(String videoName) {
        Toast.makeText(MainActivity.this,videoName,Toast.LENGTH_LONG).show();
    }
}

2018年4月12日
添加 eclipse上的java代码例子

package demo;

public class DownloadUI {

    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        DownloadUtils.instance().download(new MyDownloadListener());
        System.out.print("阻塞等待");
    }

    private static class MyDownloadListener implements DownloadListener {

        public void onDownloading(int progress) {
            // TODO Auto-generated method stub
            System.out.print("进度"+progress+"\n\r");
        }

        public void onDownloaded() {
            // TODO Auto-generated method stub
            System.out.print("完成"+"\n\r");
        }

    }

}

interface DownloadListener {
    public void onDownloading(int progress);// 下载中的函数

    public void onDownloaded();// 下载完成
}

class DownloadUtils {
    private static DownloadUtils instance = null;

    private DownloadUtils() {

    }

    public static synchronized DownloadUtils instance() {
        if (instance == null) {
            instance = new DownloadUtils();
        }
        return instance;
    }

    private boolean isDownloading=true;
    private int progress=0;

    public void download(DownloadListener listener)throws InterruptedException{
        while (isDownloading) {
            listener.onDownloading(progress);
            Thread.sleep(1000);
            progress+=10;
            if (progress>=100) {
                isDownloading=false;

            }
        }

        listener.onDownloaded();
    }

}
进度0

进度10

进度20

进度30

进度40

进度50

进度60

进度70

进度80

进度90

完成

阻塞等待

上面代码写法好像差别有点大,再改改,下面这样看起来就差不多了吧

package demo;

import demo.DownloadUtils.DownloadListener;

public class DownloadUI implements DownloadListener {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        DownloadUI downloadUI=new DownloadUI();
        DownloadUtils downloadUtils=DownloadUtils.instance();
        downloadUtils.setOnDownloadedListener(downloadUI);
        downloadUtils.download();
    }

    public void onDownloading(int progress) {
        // TODO Auto-generated method stub
        System.out.print("进度" + progress + "\n\r");
    }

    public void onDownloaded() {
        // TODO Auto-generated method stub
        System.out.print("完成" + "\n\r");
    }

}

class DownloadUtils {
    private DownloadListener mDownloadListener;

    interface DownloadListener {
        public void onDownloading(int progress);// 下载中的函数

        public void onDownloaded();// 下载完成
    }

    public void setOnDownloadingListener(DownloadListener mDownloadListener) {
        this.mDownloadListener = mDownloadListener;
    }

    public void setOnDownloadedListener(DownloadListener mDownloadListener) {
        this.mDownloadListener = mDownloadListener;
    }

    private static DownloadUtils instance = null;

    private DownloadUtils() {

    }

    public static synchronized DownloadUtils instance() {
        if (instance == null) {
            instance = new DownloadUtils();
        }
        return instance;
    }

    private boolean isDownloading = true;
    private int progress = 0;

    public void download() throws InterruptedException {
        while (isDownloading) {
            this.mDownloadListener.onDownloading(progress);
            Thread.sleep(1000);
            progress += 10;
            if (progress >= 100) {
                isDownloading = false;

            }
        }
        this.mDownloadListener.onDownloaded();
    }

}

你可能感兴趣的:(安卓)