不停地更改android通知消息并再次启动服务

时间:2012-12-04 07:49:47

标签: android

当我在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",稍后我想不停地更改它并再次启动服务。

这可能吗?

1 个答案:

答案 0 :(得分: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);
相关问题