Android事件处理的两种模型
// 标识该处理函数是否能完全处理该事件 public void onDownloaded(); //下载完成的处理函数 private DownloadUtils() { public static synchronized DownloadUtils instance() { private boolean isDownloading = true; private int progress = 0; // 实际开发中这个函数需要传入url作为参数,以获取服务器端的安装包位置 } } catch (InterruptedException e) { @Override @Override } 运行上面的模拟程序,输出如下所示: 来源:月出惊弓鸟 声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!
// 返回true,表明该函数已完全处理该事件,该事件不会传播出去
// 返回false,表明该函数未完全处理该事件,该事件会传播出去
boolean onKeyDown(int keyCode, KeyEvent event);
boolean onKeyLongPress(int keyCode, KeyEvent event);
boolean onKeyUp(int keyCode, KeyEvent event);
boolean onKeyMultiple(int keyCode, int count, KeyEvent event);
}
}
}
if (instance == null) {
instance = new DownloadUtils();
}
return instance;
}
public void download(DownloadListener listener) throws InterruptedException {
while (isDownloading) {
listener.onDownloading(progress);
// 下载过程的简单模拟
Thread.sleep(1000);
progress += 10;
if (progress >= 100) {
isDownloading = false;
}
}
// 下载完成
listener.onDownloaded();
}
e.printStackTrace();
}
}
private static class MyDownloadListener implements DownloadListener {
public void onDownloading(int progress) {
System.out.println(“下载进度是:” + progress);
}
public void onDownloaded() {
System.out.println(“下载完成”);
}
}