简体   繁体   中英

How To Resume Download When Internet is Restored or When Activity is Recreated

I am using AsyncTask to download videos from a URL link using the code below

 public static class DownloadTask extends AsyncTask<String, Void, String> {
    WeakReference<MainActivity>weakReference;

     DownloadTask(MainActivity activity) {
        weakReference = new WeakReference<>(activity);
    }

    int TIMEOUTTIME = 7000;
    int TSOCKET = 30000;
    String TAG = "TAG";

    @Override
    protected String doInBackground(String... strings) {
        MainActivity subActivity = weakReference.get();
        if (!(activity == null || activity.isFinishing())){
            try {
                URL url = new URL(subActivity.videoUrl);
                long beginTime = System.currentTimeMillis();
                Log.i(TAG, "video download starting at: " + subActivity.videoUrl);

                //Open a connection to that URL.
                URLConnection urlCon = url.openConnection();

                urlCon.setReadTimeout(TIMEOUTTIME);
                urlCon.setConnectTimeout(TSOCKET);

                InputStream is = urlCon.getInputStream();
                BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
                activity.fileTitle = "oneTooMuch TYPE.mp4";
                FileOutputStream outStream  = activity.openFileOutput(activity.fileTitle,MODE_PRIVATE);
              
                byte[] buff = new byte[5 * 1024];

                while ((len = inStream.read(buff)) != -1) {
                    outStream.write(buff, 0, len);
                }

                outStream.flush();
                outStream.close();
                inStream.close();

                Log.i(TAG, "download completed in "
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return "Out Condition";
    }

The code works well for downloading video files, but when there is a disconnection while downloading, maybe because of internet problems or onDestroy of activity, I need a way to resume the download when the internet is restored or when activity is restarted. Can anyone help with a code snippet or hint on how I can achieve this? I would really be grateful if anyone can help.

Better solution will be to use Service instead of AsyncTask . Services run separately from the application and have higher priority. You can let user continue downloading even when he closes the app, and provide him with the notification with progress, this is handy because it provides better user experience for example user can download video while watching one he already downloaded. To do this you will need to return move service into foreground and return START_REDILIVER_INTENT from onStartCommand . Also if you don't want to use Service you can use WorkManager for background operations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM