从Activity更新前台服务通知

时间:2014-10-08 14:29:26

标签: android android-service android-notifications foreground-service

我的应用程序中有一个前台服务,它在通知中有一个动态更新的文本,我可以通过使用

服务来更新它
    startForeground(Constants.FOREGROUND_ID, buildForegroundNotification());
}

private Notification buildForegroundNotification() {
    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    b.setOngoing(true);
    b.setContentIntent(getPendingIntent(this));
    b.setContentTitle(getString(R.string.app_name));
    b.setContentText(getString(R.string.time) + " " + number);
    b.setSmallIcon(R.drawable.ic_launcher);
    return b.build();
}

但是我也希望能够从Activity更新它,如何在不重新启动服务的情况下更新呢?

1 个答案:

答案 0 :(得分:1)

您可以使用此方法

final Notification notification = buildForegroundNotification();
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(Constants.FOREGROUND_ID, notification);
相关问题