6

通知バーに複数のファイルのダウンロードを表示したい場合は、キャンセルすることもできます。カスタムAndroidダウンロードサービス - ファイルごとに進捗通知の行を提供

私はAsyncTasksを使用して複数のダウンロードを並行して実行するカスタムサービスを実装しました。 OnPublishProgress私は各ファイルのダウンロードの進行状況を表示する通知バーの個々の行を更新しようとしています。 2つの堅実な日の間、私は行のちらつき、順序の入れ替え、時には空白になるか、または1つの行の更新だけで問題を修正しようとしていました。また、ルーチンをキャンセルするために行をタップしても必ずしも機能しません。ここで

は私のコードです:

protected void showProgressNotification(final File item, int progress, boolean isDownloading) { 
    String message = null; 
    int smallIcon = 0; 
    Bitmap largeIcon = null; 
    int flags = 0; 
    flags |= Notification.FLAG_ONGOING_EVENT; 
    //flags |= Notification.FLAG_FOREGROUND_SERVICE; 
    //flags |= Notification.FLAG_ONLY_ALERT_ONCE; 
    //flags |= Notification.FLAG_AUTO_CANCEL; 

    NotificationCompat.Builder builder = 
      new NotificationCompat.Builder(getApplicationContext()); 
    builder.setAutoCancel(true); 

    if (progress == 100) { 
     largeIcon = BitmapFactory.decodeResource(getResources(), 
       O2FolderListAdapter.getIconForItem(item, false)); 
     smallIcon = R.drawable.ic_cloud_upto_date; 

     if (isDownloading) { 
      message = "Download completed. Tap to clear."; 
     } else { 
      message = "Upload completed. Tap to clear."; 
     } 
    } else if (progress >= 0) { 
     largeIcon = BitmapFactory.decodeResource(getResources(), 
       O2FolderListAdapter.getIconForItem(item, true)); 
     if (isDownloading) { 
      smallIcon = R.drawable.ic_cloud_downloading; 
      message = "Downloading: " + progress + "%. Tap to cancel."; 
     } else { 
      smallIcon = R.drawable.ic_cloud_uploading; 
      message = "Uploading: " + progress + "%. Tap to cancel."; 
     } 
     builder.setProgress(100, progress, false); 
    } else { 
     largeIcon = BitmapFactory.decodeResource(getResources(), 
       O2FolderListAdapter.getIconForItem(item, true)); 
     smallIcon = R.drawable.ic_cloud_conflict; 
     if (isDownloading) 
      message = "Cancelled download. Tap to clear."; 
     else 
      message = "Cancelled upload. Tap to clear."; 
    } 

    if (mResultIntent == null) { 
     mResultIntent = new Intent(getApplicationContext(), CustomDownloadService.class); 
     mResultIntent.addFlags(Notification.FLAG_ONGOING_EVENT); 
    } 
    mResultIntent.putExtra("cancel", item.getPath().hashCode()); 
    Log.d("O2AbstractDownloadService", "Setup task id " + item.GetPath().hashCode()); 
    if (mContentIntent == null) 
     mContentIntent = PendingIntent.getService(getApplicationContext(), PI_REQ_CODE, mResultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(mContentIntent); 

    builder.setLargeIcon(largeIcon); 
    builder.setSmallIcon(smallIcon); 
    builder.setContentTitle(item.GetName()); 
    builder.setContentText(message); 

    //if (progress != 100) 
     //builder.addAction(R.drawable.ic_action_dark_cancel, "Cancel", contentIntent); 

    final Notification notification = builder.build(); 
    notification.flags = flags; 
    notification.defaults = Notification.DEFAULT_LIGHTS; 

    NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    // Id allows you to update the notification later on. 
    //mNotificationManager.notify(item.getPath().hashCode(), notification); 
    //startForeground(item.getPath().hashCode(), notification); 

    // only update notification every 100ms (unless cancel or complete) 
    long notificationDelay = 100; 
    long now = System.currentTimeMillis(); 
    if (mFutureCallTime == 0 || now > mFutureCallTime || progress == -1 || progress == 100) { 
     startForeground(item.getPath().hashCode(), notification); 
      //mNotificationManager.notify(item.GetPath().hashCode(), notification); 
    } else 
     Log.d("CustomDownloadService", "Called too often to notification"); 

    mFutureCallTime = now + notificationDelay; 
} 

だから私はダウンロードをキャンセルするために、ファイルのIDを渡し、通知をタップしたときにサービスを呼び出すために、セットアップにアクションをしようとしています。誰かが私が間違っているのを見ることができますか?私は実際に可能なのですか? Xoomタブレットでは、通知がちらつきますが、Nexus 7では頻繁に表示されることはありません。すべてのデバイスが行を常にスワップしてしまうため、ダウンロードをキャンセルすることは事実上不可能です。

アドバイスをいただければ幸いです。

UPDATE 1:スワップアウトする問題がbuilder.setWhen(fixedTime)を呼び出すことによって修正されました:UPDATE 2 Android Service.startForeground does NOT respect notification id uniqueness

私は、これは私の問題の一つの原因かもしれないと思います。明らかに、新しいdateTimeによって、リフレッシュされるたびに行の順序が変更されていました。 Xoomと 'Tap to Cancel'機能のちらつきを修正するだけです。

UPDATE 3: Xoomでの点滅が修正され、更新の呼び出しが制限されました。最後のコードは、通知が100ミリ秒に1回以上更新されるのを防ぎます。残りの問題は取り消しと関係しています。キャンセルするタップは最初に機能しますが、後続のファイルでは機能しません。また、私は行をクリアすることはできません。

更新日4:キャンセルキャンセルの問題は、resultIntentフィールドがクラスレベルであることが原因でした。私が通知をリフレッシュするたびに新しいものを作成すると、IDが縛られました。また、フラグをNotification.FLAG_ONLY_ALERT_ONCEのみに変更し、startForeground()ではなく.notify()のみを使用しました。

+0

これですべての問題は修正されましたか?もしそうなら、あなた自身の答えを掲示し、それを将来の参照のために受け入れられたものとしてマークしてください! Thnx – Entreco

答えて

1

すべての問題が修正されました。元の投稿に更新を追加しました。 要約: 1)builder.setWhen(fixedTime)に注意してください。 2)100msごとに1回以上リフレッシュしないでください 3)正しいフラグを設定してください。

+1

更新は、死に至る通知を遅らせる可能性があります。時折更新するために使用できるいくつかのメソッドがあります:http://stackoverflow.com/questions/16050381/how-do-i-get-an-android-service-to-broadcast-an-intent-every-few-秒 –

関連する問題