Commit 831b817ade8cde921bc0a4dd408f746206c0f6e7
1 parent
a3bc3ffa
--no commit message
Showing
2 changed files
with
28 additions
and
13 deletions
src/com/upc/pbe/upcnews/Descarrega.java
@@ -22,20 +22,29 @@ public class Descarrega extends AsyncTask<URL, Integer, String> | @@ -22,20 +22,29 @@ public class Descarrega extends AsyncTask<URL, Integer, String> | ||
22 | } | 22 | } |
23 | 23 | ||
24 | @Override | 24 | @Override |
25 | - protected String doInBackground(URL... urls) | ||
26 | - { | 25 | + protected void onPreExecute() { |
26 | + //Codi previ a l'execució de la tasca en background | ||
27 | + //pDialog.setMax(100); | ||
28 | + //pDialog.setProgress(0); | ||
29 | + } | ||
30 | + | ||
31 | + @Override | ||
32 | + protected String doInBackground(URL... urls){ | ||
33 | + //Codi de la tasca en background | ||
27 | String html = ""; | 34 | String html = ""; |
28 | - // Iniciem la connexi� i creem els Streams | ||
29 | - try | ||
30 | - { | 35 | + // Iniciem la connexió i creem els Streams |
36 | + try { | ||
31 | URLConnection conn = urls[0].openConnection(); | 37 | URLConnection conn = urls[0].openConnection(); |
32 | BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | 38 | BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
33 | - // Iniciem la descarrega i anem guardant al String | ||
34 | - Log.d(TAG, "\nDescarregant: \n"); | ||
35 | - Log.d(TAG, ">> URL: " + urls[0] + " >> Tamany: " + conn.getContentLength() + " bytes"); | 39 | + // Iniciem la descarrega, anem guardant al String i actualitzant la barra de progrés |
40 | + int totalLength = conn.getContentLength(); | ||
41 | + int downloaded=0; | ||
36 | String inputLine; | 42 | String inputLine; |
37 | - while ((inputLine = in.readLine()) != null) | ||
38 | - { | 43 | + Log.d(TAG, "\nDescarregant: \n"); |
44 | + Log.d(TAG, ">> URL: " + urls[0] + " >> Tamany: " + totalLength + " bytes"); | ||
45 | + while ((inputLine = in.readLine()) != null) { | ||
46 | + downloaded = downloaded + Integer.parseInt(inputLine.getBytes().toString()); | ||
47 | + publishProgress((downloaded/totalLength)*100); | ||
39 | html += inputLine + "\n"; | 48 | html += inputLine + "\n"; |
40 | } | 49 | } |
41 | in.close(); | 50 | in.close(); |
@@ -49,6 +58,11 @@ public class Descarrega extends AsyncTask<URL, Integer, String> | @@ -49,6 +58,11 @@ public class Descarrega extends AsyncTask<URL, Integer, String> | ||
49 | } | 58 | } |
50 | return html; | 59 | return html; |
51 | } | 60 | } |
52 | - | ||
53 | -} | ||
54 | - | 61 | + |
62 | + @Override | ||
63 | + protected void onProgressUpdate(Integer... values) { | ||
64 | + //Actualitza la barra de progrés de la descàrrega, respon a la crida de publishProgress | ||
65 | + int progreso = values[0].intValue(); | ||
66 | + //pDialog.setProgress(progreso); | ||
67 | + } | ||
68 | +} | ||
55 | \ No newline at end of file | 69 | \ No newline at end of file |
src/com/upc/pbe/upcnews/MainActivity.java
@@ -50,6 +50,7 @@ public class MainActivity extends Activity implements OnClickListener { | @@ -50,6 +50,7 @@ public class MainActivity extends Activity implements OnClickListener { | ||
50 | buttonDescarrega = (ImageButton) findViewById(R.id.button); | 50 | buttonDescarrega = (ImageButton) findViewById(R.id.button); |
51 | buttonDescarrega.setOnClickListener(this); | 51 | buttonDescarrega.setOnClickListener(this); |
52 | updateURL(); | 52 | updateURL(); |
53 | + //Si vitamio no està instal·lat, mostrem el missatge d'alerta | ||
53 | PackageManager pm = getPackageManager(); | 54 | PackageManager pm = getPackageManager(); |
54 | List<PackageInfo> apps = pm.getInstalledPackages(0); | 55 | List<PackageInfo> apps = pm.getInstalledPackages(0); |
55 | for(int i = 0; i < apps.size(); i++) | 56 | for(int i = 0; i < apps.size(); i++) |