Android检查连接是否中断

时间:2013-02-02 12:06:53

标签: android

我的应用需要从互联网上下载数据库。这是在AsyncTask中完成的,因此在下载数据库时,它不会显示Progress Dialog

这是数据库下载的代码:

    public void downloadDB(String dbFile, String dbFileName) {
        int count;
        try {
            URL url = new URL(dbFile);
            URLConnection conection = url.openConnection();
            conection.connect();

            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            OutputStream output = new FileOutputStream(ctx.getApplicationInfo().dataDir + "/databases/" + dbFileName);

            byte data[] = new byte[1024];

            while ((count = input.read(data)) != -1) {
                output.write(data, 0, count);
            }

            output.flush();

            output.close();
            input.close();
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
        }
    }
}

我正在进行一些测试,当数据库被下载时,我关闭了互联网连接。问题是Progress Dialog没有终止(它处于无限循环中)。我希望在关闭互联网后会有IOException,该函数返回到doInBackground的{​​{1}}方法并执行AsyncTask方法,我解除了onPostExecute

奇怪的是,如果我将手机与计算机断开连接,则进度对话框永远不会被解雇,如果我正在运行应用程序,手机连接到计算机并查看日志猫,则同样如此。如果我进入调试模式,Progress Dialog结束并返回给调用者,然后解除进度对话框。

关闭网络后,如何从downloadDB功能返回?

0 个答案:

没有答案