Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top