2016-04-03 13 views
-1

Ich möchte eine Timer-Anwendung machen, die nach Ablauf einer bestimmten Zeit einen Alarm abspielt. Ich bin neu in der Android-Entwicklung, also habe ich einen Beispiel-Timer-Code aus dem Internet genommen und versuche, ihn an meine Bedürfnisse anzupassen.Wie kann ein lauffähiger Timer unter bestimmten Bedingungen gestoppt werden?

Ich habe Teil, wo der Timer spielt einen Alarm aus, wenn 1 Minute vergangen ist abgeschlossen, aber der Alarmton hört nicht, wie ich das tun kann.

Mein Code ist unten angegeben:

package com.example.aditya.timerapp; 

import android.media.Ringtone; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.SystemClock; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.common.api.GoogleApiClient; 

public class MainActivity extends AppCompatActivity { 

private ProgressBar progress; 
private TextView timerValue; 
private long startTime = 0L; 
private Handler customHandler = new Handler(); 
long timeInMilliseconds = 0L; 
long timeSwapBuff = 0L; 
long updatedTime = 0L; 
int progressStatus = 0; 
private boolean stopped = false; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    //final CountDown timer = new CountDown(); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    timerValue = (TextView) findViewById(R.id.timerVal); 
    Button startButton = (Button) findViewById(R.id.startButton); 
    startButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      //timer.start(); 
      startTime = SystemClock.uptimeMillis(); 
      customHandler.postDelayed(updateTimerThread, 0); 

     } 
    }); 
    Button pauseButton = (Button) findViewById(R.id.stopButton); 
    pauseButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      /*try { 
       timer.wait(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      }*/ 
      timeSwapBuff += timeInMilliseconds; 
      customHandler.removeCallbacks(updateTimerThread); 
     } 
    }); 
    Button resetButton = (Button) findViewById(R.id.resetButton); 
    resetButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      //timeSwapBuff += timeInMilliseconds; 
      timeInMilliseconds = 0L; 
      timeSwapBuff = 0L; 
      updatedTime = 0L; 
      int secs = (int) (updatedTime/1000); 
      int mins = secs/60; 
      secs = secs % 60; 
      int milliseconds = (int) (updatedTime % 1000); 
      timerValue.setText("" + mins + ":" 
        + String.format("%02d", secs) + ":" 
        + String.format("%03d", milliseconds)); 
      customHandler.removeCallbacks(updateTimerThread); 
      onStop(); 

     } 
    }); 
    progress = (ProgressBar) findViewById(R.id.progressBar); 
} 

/*public void stopRun() { 
    //if (updatedTime == 0) { 
    //Toast.makeText(new MainActivity(), "StopRun",Toast.LENGTH_LONG).show(); 
    timeSwapBuff = 0; 
    timerValue.setText("00:00:000"); 
    //updateTimerThread. 
    customHandler.removeCallbacks(updateTimerThread); 
    //} 
}*/ 

private Runnable updateTimerThread = new Runnable() { 
    @Override 
    public void run() { 
     //long totalMilliseconds = 1500000; 
     //while (!stopped){ 
     //long totalMilliseconds = 15000; 
     //updatedTime = totalMilliseconds - SystemClock.currentThreadTimeMillis(); 
     timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 
     updatedTime = timeSwapBuff + timeInMilliseconds; 

     int secs = (int) (updatedTime/1000); 
     int mins = secs/60; 
     secs = secs % 60; 
     int milliseconds = (int) (updatedTime % 1000); 
     timerValue.setText("" + mins + ":" 
       + String.format("%02d", secs) + ":" 
       + String.format("%03d", milliseconds)); 
     customHandler.postDelayed(this, 0); 

     if (mins == 1 && secs == 0) { 
      playTimer(); 
     } 
    } 
}; 

public ProgressBar getProgress() { 
    return progress; 
} 

public void setProgress(ProgressBar progress) { 
    this.progress = progress; 
} 
public void playTimer(){ 
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
    Ringtone ring = RingtoneManager.getRingtone(getApplicationContext(), notification); 
    ring.play(); 
    timeSwapBuff += timeInMilliseconds; 
    customHandler.removeCallbacks(updateTimerThread); 

} 
protected void onStop() { 
    customHandler.removeCallbacks(updateTimerThread); 
    super.onStop(); 
} 
} 

So, jetzt muss ich eine Methode um den Alarm zu stoppen, wenn der Benutzer die Reset-Taste drückt. Bitte vorschlagen.

Antwort

0

Wenn ich die Frage richtig verstanden habe, können Sie versuchen, den Alarmton zu stoppen, indem Sie Ihre private Ringtone ring und ring.stop() Methode in Ihrer onStop() Methode erklären.

private Ringtone ring; //...// public void playTimer(){ Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); ring = RingtoneManager.getRingtone(getApplicationContext(), notification); ring.play(); timeSwapBuff += timeInMilliseconds; customHandler.removeCallbacks(updateTimerThread); } protected void onStop() { customHandler.removeCallbacks(updateTimerThread); ring.stop(); super.onStop(); }

1

Eine Möglichkeit, es zu tun ist, wie folgt:

  1. Definieren Sie einen boolean in Ihrem Runnable und es überprüfen, bevor irgendetwas in run() tun.

  2. Flip es eine Methode in Ihrem Runnable verwenden.

Hier ist der Code-Schnipsel:

private Runnable updateTimerThread = new Runnable() { 
    private volatile boolean flag = true; 

    public void stopTimer() { 
     this.flag = false; 
    } 

    @Override 
    public void run() { 
     while(flag) { 
      //long totalMilliseconds = 1500000; 
      //while (!stopped){ 
      //long totalMilliseconds = 15000; 
      //updatedTime = totalMilliseconds - SystemClock.currentThreadTimeMillis(); 
      timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 
      updatedTime = timeSwapBuff + timeInMilliseconds; 

      int secs = (int) (updatedTime/1000); 
      int mins = secs/60; 
      secs = secs % 60; 
      int milliseconds = (int) (updatedTime % 1000); 
      timerValue.setText("" + mins + ":" 
       + String.format("%02d", secs) + ":" 
       + String.format("%03d", milliseconds)); 
      customHandler.postDelayed(this, 0); 

      if (mins == 1 && secs == 0) { 
       playTimer(); 
      } 
     } 
    } 
}; 

Nun müssen Sie nur noch stopTimer() rufen Sie die Runnable zu stoppen.

Eine andere Möglichkeit könnte sein, die InterruptedException in der Runnable zu behandeln und einen Interrupt vom Hauptprogramm zu senden.

Verwandte Themen