2011-10-10 174 views
2

我在嘗試更新集成在通知欄中的進度條,但似乎無法使其工作。我有種想法,爲什麼它不起作用,但我不知道如何解決它。這是代碼:從IntentService更新進度條?

public class DownloadService extends IntentService{ 

    public DownloadService() { 
     super("DownloadService"); 


    } 

     @Override 
     public void onCreate() { 
      super.onCreate(); 
      ctx = getApplicationContext(); 
      root = new File(Environment.getExternalStorageDirectory()+"/folder-videos/"); 
      if(root.exists() && root.isDirectory()) { 

      }else{ 
       root.mkdir(); 
      }   
      notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, null, 0); 

      notification = new Notification(R.drawable.icon, "App", System.currentTimeMillis()); 
      contentView = new RemoteViews(getPackageName(), R.layout.progress_layout); 
      notification.flags = Notification.FLAG_AUTO_CANCEL; 
      notification.contentView = contentView; 
      notification.contentIntent = contentIntent; 
      contentView.setProgressBar(R.id.status_progress, 100, 0, false);   
      contentView.setTextViewText(R.id.status_text,"Downloading..."); 

     } 

     @Override 
    protected void onHandleIntent(Intent intent) { 
     Intent broadcastIntent = new Intent(); 

     int count; 
     String full_url = URL + intent.getStringExtra(VIDEOS); 

     try { 
      URL url = new URL(full_url); 
      URLConnection conexion = url.openConnection(); 
      conexion.connect(); 
      File file = new File(root.getPath(), intent.getStringExtra(VIDEOS)); 

      int lenghtOfFile = conexion.getContentLength(); 
      Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream(file); 

      byte data[] = new byte[1024]; 

      long total = 0; 
      contentView.setTextViewText(R.id.status_text,"Downloading " + intent.getStringExtra(VIDEOS)); 
      while ((count = input.read(data)) > 0) { 
       total += count;  
       notification.contentView.setProgressBar(R.id.status_progress, 100,(int)((total*100)/lenghtOfFile), false);  
       Log.e("totaltotal","" + (int)((total*100)/lenghtOfFile)); 
       output.write(data, 0, count); 


      } 
      notificationManager.notify(1,notification); 
      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (Exception e) { 
      Log.e("PRINTSTACK","STACK:" + e.getMessage()); 
      e.printStackTrace(); 
     } 

    } 
} 

我知道我必須調用:notificationManager.notify(1,notification);在while循環內,我試過了,但它凍結了應用程序並崩潰了。是否有任何其他方式通知通知管理器進度條更新。

謝謝!

回答

3

嘗試使用notificationManager.notify(1,notification);只有幾次

爲:

int lastProgressUpdate=0; 
while(...) 
{ 
    if(progress%5==0 && progress!=lastProgressUpdate) 
    { 

     notificationManager.notify(1,notification); 
     lastProgressUpdate=progress; 
    } 
} 
+0

5試了一下它仍然保持凍結UI。雖然嘗試了10次,但我仍然可以將通知欄拉下來,但它會變得很慢並且凍結。 – bytebiscuit

+0

是否有某種NotificationListener監聽更新並通知NotificationManager。 – bytebiscuit

+0

請嘗試更新回答 –