将字符串值转换为字符序列并在 android 中的通知中显示
convert string value to charsequence and show it in notification in android
我想在通知中显示此功能的结果。
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 序列?
String s="STR";
CharSequence cs = s; // String is already a CharSequence
所以你只需将 timeString
传递给 setContentText
编辑:
您似乎在 CountingDownTimer 开始之前调用了 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();
此处设置倒计时结束后的通知
我之前也遇到过类似的问题。您应该创建新方法并将通知放入其中。
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)
}
希望有用!
我想在通知中显示此功能的结果。
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 序列?
String s="STR";
CharSequence cs = s; // String is already a CharSequence
所以你只需将 timeString
传递给 setContentText
编辑:
您似乎在 CountingDownTimer 开始之前调用了 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();
此处设置倒计时结束后的通知
我之前也遇到过类似的问题。您应该创建新方法并将通知放入其中。
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)
}
希望有用!