verbalinsurection Posted December 26, 2010 Share Posted December 26, 2010 Bonjour, J'utilise une AsyncTask pour télécharger un gros fichier. Je l'ai mis dans un service qui doit rendre visible sa progression via une notification persistante dans la barre de notification (j'utilise pour ça une notification avec layout perso). La notificatin est bien créée, le téléchargement se fait bien, mais si j'active l'update de la notification pour mettre à jour la barre de progression l'UI se freeze complétement ! Le code OnStartCommand avec l'initialisation de la notification : notification = new Notification(R.drawable.icon, "simulating a download", System.currentTimeMillis()); notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressnotify); final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); notification.contentIntent = pendingIntent; notification.contentView.setProgressBar(R.id.progress, 100, 0, false); notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE); notificationManager.notify(42, notification); FileDownloader fileDownloader = new FileDownloader(); fileDownloader.execute(pathForDl); AsyncTask : private class FileDownloader extends AsyncTask<String, String, Boolean> { @Override protected Boolean doInBackground(String... url) { BufferedReader reader = null; FileOutputStream fos = null; InputStream in = null; Boolean retour; try { URL urlDl = new URL(url[0]); URLConnection conn = urlDl.openConnection(); FileLenght = conn.getContentLength(); if (FileLenght == -1) { throw new IOException("file error"); } in = conn.getInputStream(); reader = new BufferedReader(new InputStreamReader(in)); fos = new FileOutputStream(orLocation + "updates/" + namFileToDl); byte[] buff = new byte[1024]; long total = 0; int l = in.read(buff); while (l > 0) { total += l; publishProgress(""+(int)total/(FileLenght /100)); fos.write(buff, 0, l); l = in.read(buff); } retour = true; } catch (Exception e) { Log.e(this.getClass().getName(), "Exception while downloading file ", e); retour = false; } finally { try { fos.flush(); fos.close(); } catch (IOException e) { Log.e(this.getClass().getName(), "Exception while closing file ", e); } try { reader.close(); } catch (Exception e) { Log.e(this.getClass().getName(), "Exception while closing file ", e); } } return retour; } @Override protected void onProgressUpdate(String... progress) { notification.contentView.setProgressBar(R.id.progress, 100, Integer.parseInt(progress[0]), false); Log.d(this.getClass().getName(), "Progress " + progress[0]); notificationManager.notify(42, notification); } @Override protected void onPostExecute(Boolean result) { //continuUpdate(result); //TODO change la notifbar } } si je commente notificationManager.notify(42, notification); forcement tout se passe ien mais la barre de progression ne se met pas à jour, avez vous une idée ? Je pense que le onProgressUpdate, étant appelé à chaque paquet reçu, est bien trop souvent exécuté ce qui surcharge le système de notification. Link to comment Share on other sites More sharing options...
Pierre87 Posted December 27, 2010 Share Posted December 27, 2010 tu ne devrais appeler "publishProgress()" que si ton pourcentage augmente significativement (plus de 1%) Link to comment Share on other sites More sharing options...
verbalinsurection Posted December 28, 2010 Author Share Posted December 28, 2010 Yep, c'est ce que j'ai fait, ça marche bien mieux ;) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.