0

コンテンツのダウンロードに通知マネージャーを使用し、完了したダウンロードの割合を表示していますが、新しい割合で displaymessage 関数を呼び出すたびに、新しい通知が作成されます。毎回新しい通知を作成せずに通知を更新するにはどうすればよいですか?

public void displaymessage(String string) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Shamir Download Service";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Downloading Content:";
notification.setLatestEventInfo(context, contentTitle, string, null);
final int HELLO_ID = 2;
mNotificationManager.notify(HELLO_ID, notification);

}
4

1 に答える 1

1

私がしたことは、通知をクラスレベル変数に保存することでした..関数をcreateDownloadNotificationに変更し、上記のものを使用しますが、通知をクラス全体にアクセス可能な変数にすることを除いて.

次に、更新された情報を含む通知で setLatestEventInfo を呼び出す別の関数 (updateDownloadNotification など) を用意します。

また、mNotificationManager.notify(HELLO_ID, notification); を呼び出す必要があることに注意してください。各更新の後、または何も変更されません。

--- 更新 --- 実際には、1 つの関数だけを使用して、通知が null かどうかを確認し (そうでない場合は作成します)、それ以外の場合は既に持っているものを使用できます。

例:

public class YourClass extends Service { //or it may extend Activity

private Notification mNotification = null;

public void displaymessage(String string) {
  String ns = Context.NOTIFICATION_SERVICE;

  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  int icon = R.drawable.icon;
  CharSequence tickerText = "Shamir Download Service";
  long when = System.currentTimeMillis();
  if (mNotification == null) {
    mNotification = new Notification(icon, tickerText, when);
  }
 //mNotification.when = when;//not sure if you need to update this try it both ways
  Context context = getApplicationContext();
  CharSequence contentTitle = "Downloading Content:";
  notification.setLatestEventInfo(context, contentTitle, string, null);
  final int HELLO_ID = 2;
  mNotificationManager.notify(HELLO_ID, mNotification);

}

私のコードは、更新ごとに通知の iconLevel を更新するだけであるという点で実際には少し異なります。そのため、変更ごとに mNotification.when を更新する必要があるかどうかはわかりません。

試して見て、また報告してください。

また、この関数からいくつかの変数を作成します。通常、変数がクラスのプライベート インスタンス変数である場合は、変数に mSomething という名前を付けます。これが私がお勧めするものです:

private Notification mNotification = null;
private NotificationManager mNotificationManager = null;
private static final int HELLO_ID = 2;

public void displaymessage(String string) {
  String ns = Context.NOTIFICATION_SERVICE;
  if (mNotificationmanager == null) {
      mNotificationManager = (NotificationManager) getSystemService(ns);
  }
  int icon = R.drawable.icon;
  CharSequence tickerText = "Shamir Download Service";
  long when = System.currentTimeMillis();
  if (mNotification == null) {
    mNotification = new Notification(icon, tickerText, when);
  }
  //mNotification.when = when;//not sure if you need to update this try it both ways
  Context context = getApplicationContext();      
  notification.setLatestEventInfo(context, contentTitle, string, null);      
  mNotificationManager.notify(HELLO_ID, mNotification);

}
于 2012-02-28T20:29:18.823 に答える