How do I update the notification text for a foreground service in Android?

后端 未结 5 1967
感情败类
感情败类 2020-12-02 06:25

I have a foreground service setup in Android. I would like to update the notification text. I am creating the service as shown below.

How can I update the notifica

5条回答
  •  Happy的楠姐
    2020-12-02 07:00

    here's the code to do so in your service. Create a new notification, but ask notification manager to notify the same notification id you used in startForeground.

    Notification notify = createNotification();
    final NotificationManager notificationManager = (NotificationManager) getApplicationContext()
        .getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
    
    notificationManager.notify(ONGOING_NOTIFICATION, notify);
    

    for full sample codes, you can check here:

    https://github.com/plateaukao/AutoScreenOnOff/blob/master/src/com/danielkao/autoscreenonoff/SensorMonitorService.java

提交回复
热议问题