Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. package lazuka.agata.komunikacjasieciowa;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.IntentService;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.os.Environment;
  8. import android.util.Log;
  9. import java.io.DataInputStream;
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.net.HttpURLConnection;
  13. import java.net.URL;
  14.  
  15.  
  16. public class MyIntentService extends IntentService {
  17.     private static final String TAG = "lazuka.agata.komunikacjasieciowa";
  18.     private static final String ACTION = "lazuka.agata.komunikacjasieciowa"; //akcje, które potrafi wykonac usługa
  19.     private static final String ADDRESS = "address";  //parametr potrzebny do wykonania akcji
  20.     public static final String INFO = "download";
  21.     private static final int SIZE = 8;
  22.     private static int byteDownloaded = 0;  //zmienna przechowująca aktualną wartość pobranych bajtów
  23.     private double jump;  ///zmienna służąca do aktualizacji progress bara
  24.  
  25.     //statyczna metoda pomocnicza uruchamiająca zadanie
  26.     public void uruchomUsluge(Context context, String address) {
  27.         Intent srv = new Intent(context, MyIntentService.class);
  28.         srv.setAction(ACTION);
  29.         srv.putExtra(ADDRESS, address);
  30.         context.startService(srv);
  31.  
  32.     }
  33.  
  34.     //konstruktor
  35.     public MyIntentService() {
  36.         super("MyIntentService");
  37.     }
  38.  
  39.     //metoda wykonująca zadanie
  40.     @SuppressLint("LongLogTag")
  41.     @Override
  42.     protected void onHandleIntent(Intent intent)
  43.     {
  44.         if (intent != null)
  45.         {
  46.             final String action = intent.getAction();
  47.             if (ACTION.equals(action))
  48.             {
  49.                 final String address = intent.getStringExtra(ADDRESS);
  50.  
  51.                 wykonajZadanie(address);
  52.  
  53.                 Log.d("end", "Zakonczono");
  54.             } else {
  55.                 Log.e(TAG,"Nieznana akcja");
  56.             }
  57.         }
  58.         Log.d(TAG,"Usługa wykonała zadanie");
  59.     }
  60.  
  61.     private void wykonajZadanie(String address) {
  62.  
  63.         //nawiązywanie połączęnia HTTP z podanym adresem URL
  64.         HttpURLConnection connection = null;
  65.         FileOutputStream strumienDoPliku = null;
  66.         int progress = 0;
  67.         try
  68.         {
  69.             URL url = new URL(address);
  70.             connection = (HttpURLConnection) url.openConnection();
  71.             connection.setRequestMethod("GET");
  72.             connection.setDoOutput(true);
  73.  
  74.             //zapisanie danych do pliku
  75.             File testFile = new File(url.getFile());
  76.             File outputFile = new File(
  77.                     Environment.getExternalStorageDirectory() +
  78.                             File.separator+ testFile.getName());
  79.             if (outputFile.exists()) outputFile.delete();
  80.  
  81.             //odbieranie danych z połączenia sieciowego
  82.             DataInputStream czytnik = new DataInputStream(connection.getInputStream());
  83.             strumienDoPliku = new FileOutputStream(outputFile.getPath());
  84.             byte bufor[] = new byte[SIZE];
  85.             int pobrano = czytnik.read(bufor, 0, SIZE);
  86.             int lengthOfFile = connection.getContentLength();  //pobranie rozmiaru pliku
  87.             Intent zamiar = new Intent(INFO);
  88.             zamiar.putExtra("obiekt", new PostepInfo(0, 1));
  89.             sendBroadcast(zamiar);
  90.             byteDownloaded = 0; //inicjalizacja zmiennej przechowującej aktualną wartość pobranych bajtów
  91.             jump = 0; //inicjalizacja zmiennej służącej do aktualizacji progress bara
  92.  
  93.             while (pobrano != -1) {
  94.                 strumienDoPliku.write(bufor, 0, pobrano);
  95.                 byteDownloaded += pobrano;
  96.                 pobrano = czytnik.read(bufor, 0, SIZE);
  97.  
  98.                 //pętla dzięki której progress bar będzie przeskakiwał co 10%
  99.                 if(byteDownloaded >= jump) {
  100.                     progress = (int) byteDownloaded * 100 / lengthOfFile;
  101.                     zamiar.putExtra("obiekt", new PostepInfo(byteDownloaded / 1000, progress));  //dzielimy bajty przez 1000 żeby były kB
  102.                     sendBroadcast(zamiar);
  103.                     jump+=lengthOfFile*0.1;  //dodanie do zmiennej jump 10% wielkości pliku
  104.                 }
  105.             }
  106.  
  107.         }
  108.         catch (Exception e)
  109.         {
  110.             e.printStackTrace();
  111.         }
  112.         finally
  113.         {
  114.             if (connection != null) connection.disconnect();
  115.         }
  116.  
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement