2017-08-29 69 views
0

我正在從服務器上下載視頻。我在通知中顯示下載的進度。一旦下載完成,我會在我的通知中顯示下載完成,但也會看到顯示進度的通知。如何擺脫顯示進度的通知,一旦下載complete.Here是我的代碼AsyncTask生成兩個通知

public class VideoAsyncTask extends AsyncTask<Object, Integer, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      notificationManager = (NotificationManager) 
        getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 

      notificationBuilder = new NotificationCompat.Builder(getApplicationContext()) 
        .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setContentText("Download in progress") 
        // .setContentIntent(contentIntent(getApplicationContext())) 
        .setAutoCancel(true); 
      notificationBuilder.setProgress(100, 0, false); 

      notificationManager.notify(1, notificationBuilder.build()); 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 
      notificationBuilder.setProgress(100, values[0], false); 
      notificationManager.notify(id, notificationBuilder.build()); 
      super.onProgressUpdate(values); 
     } 

@Override 
protected Void doInBackground(Object... params) { 

    try { 

     URL url = new URL(params[0].toString()); 
     HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
     c.setRequestMethod("GET"); 
     c.connect(); 


     String PATH = Environment.getExternalStorageDirectory().toString(); 
     File file = new File(PATH + "/SavedVideos"); 
     try { 
      file.mkdirs(); 

     } catch (Exception e) { 

     } 

     if (!contacts.getVideoName().contains(".mp4")) { 
      contacts.getVideoName().concat(".mp4"); 
     } 


     File outputFile = new File(file, contacts.getVideoName()); 
     if (!outputFile.exists()) { 
      outputFile.createNewFile(); 
     } 


     FileOutputStream fos = new FileOutputStream(outputFile); 

     InputStream is = c.getInputStream(); 

     byte[] buffer = new byte[1024]; 
     int len1 = 0; 
     publishProgress(Math.min(buffer.length, 100)); 
     while ((len1 = is.read(buffer)) != -1) { 

      fos.write(buffer, 0, len1); 
     } 
     fos.close(); 
     is.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    /* for (int i = 0; i <= 100; i += 5) { 
     // Sets the progress indicator completion percentage 
     publishProgress(Math.min(i, 100)); 
     try { 
      // Sleep for 5 seconds 
      Thread.sleep(2 * 1000); 
     } catch (InterruptedException e) { 
      Log.d("Failure", "sleeping failure"); 
     } 
    }*/ 


    return null; 
} 

@Override 
protected void onPostExecute(Void aVoid) { 
    super.onPostExecute(aVoid); 


    notificationBuilder.setContentText("Download Finished"); 
    notificationBuilder.setProgress(0, 0, false); 

    notificationManager.notify(1, notificationBuilder.build()); 
} 

}

+0

使用ID –

+0

notificationManager.cancel(NOTIFICATION_ID)取消正在進行的通知; –

+0

我在下載時看到兩個通知。當下載完成時,它工作正常 – Pritish

回答

1

對於您在AsyncTask中構建的所有通知,使用相同的通知ID,這樣每個通知都會替換之前的通知。爲了讓您正在尋找的行爲,做如下改變:

@Override 
protected void onProgressUpdate(Integer... values) { 
    notificationBuilder.setProgress(100, values[0], false); 
    //replace this line 
    //notificationManager.notify(id, notificationBuilder.build()); 
    //by this one 
    notificationManager.notify(1, notificationBuilder.build()); 
    super.onProgressUpdate(values); 
} 
+0

它的工作,但顯示進度對我來說是一個問題。應該是publishProgress中的第一個參數(Math.min(buffer.length,100));我知道buffer.length是錯的 – Pritish

+0

@abu這是另一個不同的問題,你應該問一個新的問題 – AlexTa

1

我想你,因爲生成你的onPreExecute()兩個通知和onPostExecute()你使用1作爲您的notification id。然後在onProgressUpdate()中,您使用的是變量id,該變量未顯示分配給其的數字,可能不是1,因此它會生成2個通知,因爲您在兩個不同的id上調用了通知。