2017-08-29 4 views
0

私はサーバーからビデオをダウンロードしています。通知のダウンロードの進行状況を表示しています。ダウンロードが完了するとすぐに私の通知にダウンロード完了が表示されますが、進行状況を示す通知も表示されます。どのように進行状況を示す通知を取り除くために、ダウンロードはcomplete.Hereが私のコードAsyncTaskは2つの通知を生成します

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

ダウンロード時に2つの通知が表示されます。ダウンロードが完了したら正常に動作します。 – 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

進行中ですが、私には問題があります。私はbuffer.lengthが間違っていることを知っています – Pritish

+0

@abuそれは別の異なる質問です、あなたは新しい質問として尋ねるべきです – AlexTa

1

です一度私はあなたがあるため、あなたのonPreExecute()に生成された2つの通知があり、onPostExecute()あなたのように1を使用していると思いますあなたのnotification idそれでonProgressUpdate()ではidという変数を使用していますが、割り当てられた番号は表示されません。1であるため、2つの異なるidの通知を呼び出すので2つの通知が生成されます。

関連する問題