1

私はハニカム用に開発しており、何日もこの問題を解決しようとしています。私は意図のない通知サービスを持っています (必要ありません)。問題は、displaymessage関数を呼び出すたびに毎回通知が表示されるため、100 個の通知を受け取ることです。一度だけポップアップし、その後はパーセントのテキストのみを変更したいと思います。マーケット プログレス バーとパーセンテージからのダウンロードに似ています。関数を分離し、新しいテスト コードを作成しましたが、成功しませんでした。これを別の角度から見ると、新しい通知を作成せずに、既存の通知のテキストを変更したいと考えています。手伝ってくれませんか?

これがコード全体です(分離後):

package com.disp;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;

public class DispalyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        for (int i = 0; i < 100; i++) {
            SystemClock.sleep(300);
            displaymessage(""+i+"%");

        }
    }

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

    }
}
4

1 に答える 1

1

各通知は NotificationManager によって整数 ID で一意に識別されるため、setLatestEventInfo() を新しい値で呼び出して通知を修正し、通知のいくつかのフィールド値を変更してから、notify() を再度呼び出すことができます。

オブジェクト メンバー フィールドを使用して各プロパティを変更できます (Context および通知のタイトルとテキストを除く)。contentTitle と contentText に新しい値を指定して setLatestEventInfo() を呼び出して通知を更新するときは、常にテキスト メッセージを修正する必要があります。次に、notify() を呼び出して通知を更新します。(もちろん、カスタム通知レイアウトを作成した場合は、これらのタイトルとテキストの値を更新しても効果はありません。)

から

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

于 2012-03-01T18:19:10.113 に答える