Search code examples
androidstringnotificationscharsequence

convert string value to charsequence and show it in notification in android


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?


Solution

  • 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!