1

我正在为 Honeycomb 开发,几天来我一直在努力解决这个问题。我有一个没有意图的通知服务(不需要),问题是每次调用displaymessage功能后,通知 pup-up 每次,所以我收到 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

因为每个通知都由具有整数 ID 的 NotificationManager 唯一标识,所以您可以通过使用新值调用 setLatestEventInfo() 来修改通知,更改通知的某些字段值,然后再次调用 notify()。

您可以使用对象成员字段修改每个属性(上下文和通知标题和文本除外)。当您通过调用 setLatestEventInfo() 并使用 contentTitle 和 contentText 的新值来更新通知时,您应该始终修改文本消息。然后调用 notify() 来更新通知。(当然,如果您创建了自定义通知布局,则更新这些标题和文本值无效。)

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

于 2012-03-01T18:19:10.113 回答