Android Xamarin:每秒更新一次通知文本

Android Xamarin:每秒更新一次通知文本,android,xamarin,Android,Xamarin,这是在我的服务类中发生的通知: void DispatchNotificationThatServiceIsRunning() { Notification.Builder notificationBuilder = new Notification.Builder(this) .SetSmallIcon(Resource.Drawable.btn_start) .SetContentTitle("Count Dow

这是在我的服务类中发生的通知:

    void DispatchNotificationThatServiceIsRunning()
    {
        Notification.Builder notificationBuilder = new Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.btn_start)
            .SetContentTitle("Count Down Active!")
            .SetContentText(_2TimerRunning._hours.ToString() + _2TimerRunning._minutes.ToString() 
                            + _2TimerRunning._seconds.ToString());

        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.Notify(1000, notificationBuilder.Build());

    }
这确实有效,但问题是“SetContentText”部分。 此文本仅更新一次,但另一个计时器在大括号内不断倒数秒数。不幸的是,它没有得到更新,即使计时器仍在计数,通知也总是读“000000”

如何每秒更新一次文本,以便在通知中持续倒计时

另外,如何捕获单击此通知以再次打开活动?谢谢:)


谢谢

问题似乎出在
\u 2TimerRunning
从该对象检索的时间值无效。我所看到的所有其他代码都运行良好。因此,我的建议是首先调试该对象

Xamarin:每秒更新一次通知文本

您可以创建一个任务,它可以帮助您每1秒更新一次通知文本,在
Task中调用
DispatchNotificationThatServiceIsRunning()
。Delay()
可以解决重复问题:

void DispatchNotificationThatServiceIsRunning()
{
    Task.Delay(1000).ContinueWith(t =>
    {
        Notification.Builder notificationBuilder = new Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.Icon)
            .SetContentTitle("Count Down Active!")
            .SetContentText(DateTime.Now.ToString("hh:mm:ss"));

        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.Notify(1000, notificationBuilder.Build());
        DispatchNotificationThatServiceIsRunning();//This is for repeate every 1s.
    }, TaskScheduler.FromCurrentSynchronizationContext());
}

更新:

您可以使用来实现此功能,添加以下代码:

 Intent notificationIntent = new Intent(ApplicationContext, typeof(Activity2));
 PendingIntent contentIntent = PendingIntent.GetActivity(this, 1, notificationIntent, PendingIntentFlags.UpdateCurrent);
 ...
 notificationBuilder.SetContentIntent(contentIntent).SetAutoCancel(true);

你每秒钟都收到通知吗?是的是的,什么是u
2时间间隔
?难以置信的好答案!还有一个问题:我怎样才能点击notifaction打开一个活动?非常感谢!再次感谢,我不知道如何实现,是否有一个“.点击”通知?好的,谢谢,我让notificationmanager在全班都可以使用,使它工作起来了!:)@innomotion媒体,快乐编码:)