android事件监听的两种方式

Android事件处理的两种模型

   // 标识该处理函数是否能完全处理该事件
// 返回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);
}

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(“下载完成”);
}

}

运行上面的模拟程序,输出如下所示:

android事件监听的两种方式

来源:月出惊弓鸟

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2013年10月6日
下一篇 2013年10月6日

相关推荐