2012-02-28 10 views
0

コンテンツのダウンロードに通知マネージャを使用していますが、完了したダウンロードの割合を表示していますが、新しいパーセントでdisplaymessage関数を呼び出すたびに新しい通知が作成されます。毎回新しい通知を作成せずに通知を更新するだけですか?私が何をしたかAndroid通知マネージャの更新率

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); 

} 
+0

全くディックことや、あなたの疑問を回避するが、[DownloadManager](http://developer.android.com/reference/android/app/DownloadManagerを見てみません.html) – hwrdprkns

答えて

1

は はたぶんcreateDownloadNotificationにあなたの関数を変更し、クラス全体への通知は、変数にアクセスできるよう除いて、あなたは上記の持っているものを使用します..クラスレベル変数に通知を格納しました。

次に、updateDownloadNotificationのような別の関数を使用して、通知にsetLatestEventInfoを呼び出し、更新された情報を返します。

また、mNotificationManager.notify(HELLO_ID、通知)を呼び出す必要があります。各更新後には何も変わらない。

---アップデート--- 実際には1つの機能しか持たず、通知がヌルかどうかを確認することもできます(そうでない場合は作成します)。

例:

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ですよ、実際にその中に少し異なっているので、あなたが更新する必要がある場合、私はそれぞれの変更であればわからないんだけど告知.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); 

} 
+0

答えをありがとう、私はelse文の場合は、{上のコード}、 else { notification.setLatestEventInfo(context、contentTitle、string、null); \t mNotificationManager.notify(HELLO_ID、通知);} しかし、うまくいきませんでした。 – DanM

関連する問題