1

我想在通知中显示此功能的结果。

public class TimerService extends Service {
    public String timeString;
... // service methodd
public class CountingDownTimer extends CountDownTimer{
             public CountingDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long leftTimeInMilliseconds) {

            timeString = String.format("%02d", 5000/ 60)
                    + ":" + String.format("%02d", 5000% 60);
                ...
        }
...// at the end of TimerService class
                    notification = new NotificationCompat.Builder(this)
                    .setContentText(timeString).build();

但不幸的是,通知中没有显示(null)。我能做些什么?如何将字符串值转换为字符序列?

4

2 回答 2

1

我以前也有类似的问题。您应该创建新方法并将通知放入其中。

private void setupNotification(String s) {}

最重要的是你应该发送timestringfromCountingDownTimersetupNotification。所以这样做:

public class CountingDownTimer extends CountDownTimer{
    public String timeString=null;

         public CountingDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long leftTimeInMilliseconds) {
        timeString = String.format("%02d", 5000/ 60)
                + ":" + String.format("%02d", 5000% 60);
       setupNotification(timeString);

    }

private void setupNotification(String s) {
    notification = new NotificationCompat.Builder(this)
                .setContentText(s)
}

我希望它有效!

于 2016-12-16T12:23:52.327 回答
0
String s="STR";
CharSequence cs = s;  // String is already a CharSequence

所以你只需传递timeStringsetContentText

编辑:

看来您在 CountingDownTimerstarts 之前调用了 notification.setContentText()。

notification 在里面打电话OnFinish()

 public CountingDownTimer(long millisInFuture, long countDownInterval) {
        @Override
        public void onTick(long l) {
            timeString = String.format("%02d", l / 60)
                    + ":" + String.format("%02d", l % 60);

        // Add Here
        notification = new NotificationCompat.Builder(this)
                             .setContentText(timeString).build();

        }

        @Override
        public void onFinish() {

        }
    }.start();

倒数计时器完成后设置的此处通知

于 2016-12-14T12:48:27.320 回答