0

I want to show the result of this function in notification.

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();

but unfortunately nothing(null) show in the notification. what can I do? how can I convert String value to char sequence?

Migitanar
  • 31
  • 8

2 Answers2

1

I had similar problem before. you should create new method and put notification in it.

private void setupNotification(String s) {}

The most important thing is that you should send timestring from CountingDownTimer to setupNotification. so do it like this:

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)
}

I hope it works!

0
String s="STR";
CharSequence cs = s;  // String is already a CharSequence

so you just pass timeString to setContentText

Edit:

it seems you call notification.setContentText() before CountingDownTimerstarts.

call notification inside 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();

here notification set after Countdown timer finished

sivaBE35
  • 1,876
  • 18
  • 23