Sajmon

DownloadIntentService

Feb 13th, 2013
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. package com.sajmon.imagedownload;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8.  
  9. import android.app.IntentService;
  10. import android.content.Intent;
  11. import android.os.Bundle;
  12. import android.os.ResultReceiver;
  13.  
  14. public class DownloadService extends IntentService {
  15.    
  16.     protected final byte[] BUFFER = new byte[1024];
  17.     protected static final int PROGRESS_UPDATE = 1345;
  18.    
  19.     public DownloadService() {
  20.         super("DownloadService");
  21.     }
  22.  
  23.     protected void onHandleIntent(Intent intent) { 
  24.         String urlLink = intent.getStringExtra("url");
  25.         ResultReceiver receiver = intent.getParcelableExtra("receiver");
  26.         HttpURLConnection con = null;
  27.         InputStream input = null;
  28.         long contentLength;
  29.         long total = 0;
  30.         int downloaded = 0;
  31.         long start = 0, end = 0;
  32.         start = System.currentTimeMillis();
  33.         try {
  34.             URL url = new URL(urlLink);
  35.             con = (HttpURLConnection) url.openConnection();
  36.             con.connect();
  37.             if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
  38.                 contentLength = con.getContentLength();
  39.                 input = con.getInputStream();
  40.                 while ((downloaded = input.read(BUFFER)) != -1) {
  41.                     total += downloaded;
  42.                     Bundle data = new Bundle();
  43.                     data.putInt("progress", (int) (total * 100 / contentLength));
  44.                     //publishing progress
  45.                     receiver.send(PROGRESS_UPDATE, data);
  46.                 }
  47.                 end = System.currentTimeMillis();
  48.                 Bundle data = new Bundle();
  49.                 data.putInt("progress", 100);
  50.                 data.putLong("time", end - start);
  51.                 receiver.send(PROGRESS_UPDATE, data);
  52.             }
  53.         }
  54.         catch (MalformedURLException e) {
  55.             e.printStackTrace();
  56.         }
  57.         catch (IOException e) {
  58.             e.printStackTrace();
  59.         }
  60.         finally {
  61.             if (input != null) {
  62.                 try {
  63.                     input.close();
  64.                 }
  65.                 catch (IOException e) {
  66.                     e.printStackTrace();
  67.                 }
  68.             }
  69.             if (con != null) {
  70.                 con.disconnect();
  71.             }
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment