2016-12-14 3 views
1

Ich möchte das Ergebnis dieser Funktion in der Benachrichtigung anzeigen.Konvertieren Zeichenfolge Wert in Charsequenz und zeigen Sie es in der Benachrichtigung 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(); 

aber leider nichts (Null) in der Benachrichtigung anzeigen. Was kann ich tun? Wie kann ich String-Wert in Char-Sequenz konvertieren?

+0

aktualisiert ans .......... – siva35

Antwort

1

ich vor ähnliches Problem hatte. Sie sollten eine neue Methode erstellen und eine Benachrichtigung darin einfügen.

private void setupNotification(String s) {} 

Das Wichtigste ist, dass man timestring von CountingDownTimer zu setupNotification senden soll. so tun es wie folgt aus:

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

Ich hoffe, es funktioniert!

+0

Danke! Ihre Antwort funktioniert. – Migitanar

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

so passieren Sie nur timeString zu setContentText

Edit:

es scheint, dass Sie rufen notification.setContentText() vor CountingDownTimerstarts.

Anruf notification innen 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(); 

hier Benachrichtigung nach Countdown Timer beendet

+0

Ich aktualisiere meine Frage und poste einige Details. würdest du es bitte überprüfen. – Migitanar

+0

oh nein! Ich möchte den Zähler während der Zählung in der Benachrichtigung anzeigen. – Migitanar

Verwandte Themen