Android事件处理的两种模型
public void onDownloaded(); //下载完成的处理函数
}
private DownloadUtils() {
}
public static synchronized DownloadUtils instance() {
if (instance == null) {
instance = new DownloadUtils();
}
return instance;
}
private boolean isDownloading = true;
private int progress = 0;
// 实际开发中这个函数需要传入url作为参数,以获取服务器端的安装包位置
public void download(DownloadListener listener) throws InterruptedException {
while (isDownloading) {
listener.onDownloading(progress);
// 下载过程的简单模拟
Thread.sleep(1000);
progress += 10;
if (progress >= 100) {
isDownloading = false;
}
}
// 下载完成
listener.onDownloaded();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static class MyDownloadListener implements DownloadListener {
@Override
public void onDownloading(int progress) {
System.out.println(“下载进度是:” + progress);
}
@Override
public void onDownloaded() {
System.out.println(“下载完成”);
}
}
}
运行上面的模拟程序,输出如下所示:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!