【问题标题】:convert string value to charsequence and show it in notification in android将字符串值转换为字符序列并在 android 的通知中显示
【发布时间】:2016-12-14 12:31:35
【问题描述】:

我想在通知中显示这个函数的结果。

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)。我能做些什么?如何将字符串值转换为字符序列?

【问题讨论】:

  • 更新答案........

标签: android string notifications charsequence


【解决方案1】:

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

private void setupNotification(String s) {}

最重要的是你应该将timestringCountingDownTimer发送到setupNotification。所以这样做:

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

我希望它有效!

【讨论】:

    【解决方案2】:
    String s="STR";
    CharSequence cs = s;  // String is already a CharSequence
    

    所以你只需将timeString 传递给setContentText

    编辑:

    您似乎在 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();
    

    此处设置倒数计时器结束后的通知

    【讨论】:

    • 我更新了我的问题并发布了一些详细信息。请你检查一下。
    • 哦不!我想在计数期间在通知中显示计数器。
    猜你喜欢
    • 2017-07-11
    • 2012-11-09
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多