Android:下载期间强制关闭连接更改

发布于 2024-12-10 16:37:12 字数 753 浏览 0 评论 0原文

因此,我正在使用 Asynctask 加载 URL,并且在下载开始之前已经处理了所有可能的连接问题,但现在在下载时从数据切换到 wifi 时,我的应用程序强制关闭。所以问题是:

我是否必须在服务上执行异步任务,或者是否有办法暂停它并在有可用连接时恢复?我已经有一个接收器,它在连接后立即开始下载:

    if (isOnline()) {
        getData();
    } else {
        IntentFilter intentFilter = new IntentFilter(
                "android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                if (isOnline()) {
                    unregisterReceiver(this);
                    getData();
                }
            }
        }, intentFilter);
    }

但是如何在连接可用后恢复 Asynctask?

So, I'm loading an URL with Asynctask and I've handled all the possible connectivity problems before the download starts, but now my app force closes when switching from data to wifi while downloading. So the question is:

Will I have to do the Asynctask on a Service or is there a way pausing it and resuming when there is a connection available? I already have a receiver which starts downloading right after there is connectivity:

    if (isOnline()) {
        getData();
    } else {
        IntentFilter intentFilter = new IntentFilter(
                "android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                if (isOnline()) {
                    unregisterReceiver(this);
                    getData();
                }
            }
        }, intentFilter);
    }

But how can I resume Asynctask after connectivity is available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡 2024-12-17 16:37:13

这绝对是您想要在服务后台执行的操作。我是否可以建议查看 IntentService 类的文档?通过在服务中执行此操作,您不必费尽心思确保清理 Activity 中的 AsyncTask 内容。并且该服务将继续在后台运行,直到下载完成。

This is definitely you want to do in the background on a service. Might I suggest looking at the documentation for the IntentService class? By doing this in a service, you don't have to mess around with all the nastiness of making sure to you cleanup the AsyncTask stuff in the Activity. And the service will continue to run in the background until the download is finished.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文