我想在通知中显示此功能的结果。
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)。我能做什么?如何将String值转换为char序列?
答案 0 :(得分:1)
之前我遇到过类似的问题。你应该创建新方法并在其中放置通知。
private void setupNotification(String s) {}
最重要的是,您应该从timestring
向CountingDownTimer
发送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)
}
我希望它有效!
答案 1 :(得分:0)
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();
此处倒数计时器完成后的通知设置