6

这是我在 android 上Notification启动时的方法:Service

private void showNotification() {
        Notification notification = new Notification(R.drawable.call, "Some text", System.currentTimeMillis());
        Intent intent = new Intent(this, MainActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        notification.setLatestEventInfo(this, getString(R.string.notification_label), getString(R.string.notification_text_short), pi);
        notification.flags |= Notification.FLAG_NO_CLEAR;
        startForeground(7331, notification);
    }

以后可以改文字吗。现在是例如"Some text",以后我想更改它而无需停止并再次启动服务。

那可能吗?

4

1 回答 1

9

您正在使用通知 ID 7331 来显示通知以及启动服务。只要您发送具有相同 ID 的新通知,它将替换旧通知。

使用它来更新通知

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(ns);
long when = System.currentTimeMillis();

Notification notification = new Notification("your icon", "your ticker text", when);
/*<set your intents here>*/
mNotificationManager.notify(7331, notification);
于 2012-12-04T08:33:21.850 回答