2012-01-24 5 views
0

こんにちは私はダウンロードファイルのクラスを持っています。彼は、ダウンロードが終了したときに削除通知を除き、期待どおりに動作します。私は、コードの2つの同一のセクションが実際に異なるものを与えることに注意します。私は今、私は次のような行動を取っていることを意味します:私はファイルをダウンロードし、通知をクリックしてキャンセルすることができます。ダウンロードは中止する必要があります。しかし、ダウンロードが完了するまで待ちます。最初に通知が終了すると、2番目の通知が中止され、最初の通知は永久に通知バーに残ります。私はどこが間違っているのか分かりません。 例コード:ファイルダウンロードの悪い動作の通知android

public class DownloadVkVideoFiles extends AsyncTask<String, Integer, String>{ 
    private static String BROADCAST_ACTION = "com.yourshows.helper.DownloadVkVideoFile.CANCELID"; 

    public DownloadVkVideoFiles(Context c, String title, int taskId) { 
    this.context = c; 
    this.notifyId = taskId; //this is unique notification Id 
    this.BROADCAST_ACTION += String.valueOf(taskId); //broadcast action for pending intent 
    } 

    @Override 
    protected void onPreExecute() { 
     // execute the status bar notification 
     createNotification(); 
     super.onPreExecute(); 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(BROADCAST_ACTION); 
     context.registerReceiver(receiver, filter); //register Receiver for cancel download file 
    } 

    @Override 
    protected String doInBackground(String... params) { //download file} 

    @Override 
    public void onProgressUpdate(Integer... progress) { 
     notification.contentView.setProgressBar(R.id.progressBar, 100, progress[0], false); 
     // inform the progress bar of updates in progress 
     notificationManager.notify(notifyId, notification); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     notificationManager.cancel(notifyId); // close finished notification 
     unregisterReceiver(); 
     LOG.send(LOG.I, TAG, "Notification finished with ID = " + notifyId); 
    } 

    @Override 
    protected void onCancelled() { 
     super.onCancelled(); 
     notificationManager.cancel(notifyId); 
     unregisterReceiver(); 

     LOG.send(LOG.I, TAG, "Notification finished with ID = " + notifyId); 
    } 
} 

UPD: createNotification:

public void createNotification() { 
     notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
     Intent notificationIntent = new Intent(); 
     notificationIntent.setAction(BROADCAST_ACTION); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 
      PendingIntent.FLAG_CANCEL_CURRENT); 

     RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.download_notification); 

     // TODO change to shows title 
     tickerText = context.getResources().getText(R.string.downloadTitle); 
     icon = android.R.drawable.stat_sys_download; 
     time = System.currentTimeMillis(); 

     notification = new Notification(icon, tickerText, time); 
     notification.flags |= Notification.FLAG_ONGOING_EVENT; 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 

     contentView.setImageViewResource(R.id.downloadImage, R.drawable.download); 
     contentView.setTextColor(R.id.title, notification_text_color); 
     contentView.setFloat(R.id.title, "setTextSize", notification_text_size - 3); 
     contentView.setTextViewText(R.id.title, title); 
     contentView.setProgressBar(R.id.progressBar, 100, 0, false); 

     notification.contentIntent = pendingIntent; 
     notification.contentView = contentView; 
     //notificationManager.notify(notifyId, notification); 
     service.startForeground(notifyId, notification); 
    } 

答えて

0

私は私のプロジェクトではそのような何かを試してみましたが、あなたがダウンロードしようとした場合に5つ以上は、ダウンロードファイルが予期しない動作しますファイルすべてのファイルが壊れています。 私はAsyncTaskではなくServiceに構造を変更します。 ダウンロードは、1つずつダウンロード中のキューに保存されています。通知バーには、ダウンロードの進行状況バー番号と現在のファイル名とダウンロード率が含まれています。

+0

私は一度に6つのタスクを開始し、すべてが正常にダウンロードされました。しかし問題は、通知バーにある5つの通知がダウンロードタスクであっても終了したことです。 – Mrusful

+0

私は5つのファイルをダウンロードしようとしましたが、それぞれ約5 MBのファイルが破損していました。 作成通知機能を追加してください。 – Basbous

+0

私の更新を参照してください。私もダメージファイルを持っていましたが、それは私の失敗です。私は同じ名前のファイルがあったときに起こった。ファイルが互いに上書きされました。とにかく私は本当にあなたが提案したようなこの機能をより良くするだろうと思う。それは私の問題を避けることができます。 – Mrusful