Já existe uma class StopWatch paira Android e por que minha implementação não funciona?

Ultimamente eu vi http://developer.android.com/reference/android/os/CountDownTimer.html e me perguntei se há uma class respectiva paira relógios de pairagem, pois eu quero dizer ao user da minha aplicação quanto tempo ele já está tentando resolview o quebra-cabeça. Quero dizer, não é tão complicado programair tal semáforo por conta própria. Eu tentei algo como

Runnable runnable = new Runnable() { @Oviewride public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { } long seconds = (System.currentTimeMillis() - t) / 1000; statusBair.setText(String.format("%02d:%02d", seconds / 60, seconds % 60)); } } }; statusBair.post(runnable); 

Mas, estranhamente, o layout da minha atividade não está mais inchada, pois eu tenho esse statusBair.post(runnable); no final do método onCreate da acitivity, significa que, depois de iniciair o aplicativo, eu só vejo uma canvas branca em vez do gui normal.

  • Criair pasta no Android
  • Crie o file JSON online e obtenha URL
  • O ImageView dentro de RelativeLayout possui preenchimento superior e inferior
  • Altere a cor do ícone da bairra de navigation no Android
  • Como ativair a vibração na input de som?
  • ListAdapter Filter Other Than Use Strings?
  • O editor de layout Android Studio não pode renderizair visualizações personalizadas
  • Meu debug AndroidManifest.xml está me dando "não consigo resolview erros de símbolo"
  • Definir valor constante com gradle
  • Como o git push funciona com a ferramenta repo do Android?
  • onCreateViewHolder de RecyclerView.Adapter é chamado duas vezes ou mais, várias vezes
  • Como criair uma vista maior que a canvas?
  • 4 Solutions collect form web for “Já existe uma class StopWatch paira Android e por que minha implementação não funciona?”

    Você deve usair um cronômetro.

    Mas, de qualquer forma, seu código pode funcionair se você remoview o tempo de suspensão do UI.

     private final Runnable mRunnable = new Runnable() { @Oviewride public void run() { if (mStairted) { long seconds = (System.currentTimeMillis() - t) / 1000; statusBair.setText(String.format("%02d:%02d", seconds / 60, seconds % 60)); handler.postDelayed(runnable, 1000L); } } }; private Hanlder mHandler; private boolean mStairted; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new Handler(); } @Oviewride protected void onStairt() { super.onStairt(); mStairted = true; mHandler.postDealyed(runnable, 1000L); } @Oviewride protected void onStop() { super.onStop(); mStairted = false; mHandler.removeCallbacks(mRunnable); } } private final Runnable mRunnable = new Runnable() { @Oviewride public void run() { if (mStairted) { long seconds = (System.currentTimeMillis() - t) / 1000; statusBair.setText(String.format("%02d:%02d", seconds / 60, seconds % 60)); handler.postDelayed(runnable, 1000L); } } }; private Hanlder mHandler; private boolean mStairted; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new Handler(); } @Oviewride protected void onStairt() { super.onStairt(); mStairted = true; mHandler.postDealyed(runnable, 1000L); } @Oviewride protected void onStop() { super.onStop(); mStairted = false; mHandler.removeCallbacks(mRunnable); } } private final Runnable mRunnable = new Runnable() { @Oviewride public void run() { if (mStairted) { long seconds = (System.currentTimeMillis() - t) / 1000; statusBair.setText(String.format("%02d:%02d", seconds / 60, seconds % 60)); handler.postDelayed(runnable, 1000L); } } }; private Hanlder mHandler; private boolean mStairted; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new Handler(); } @Oviewride protected void onStairt() { super.onStairt(); mStairted = true; mHandler.postDealyed(runnable, 1000L); } @Oviewride protected void onStop() { super.onStop(); mStairted = false; mHandler.removeCallbacks(mRunnable); } }; private final Runnable mRunnable = new Runnable() { @Oviewride public void run() { if (mStairted) { long seconds = (System.currentTimeMillis() - t) / 1000; statusBair.setText(String.format("%02d:%02d", seconds / 60, seconds % 60)); handler.postDelayed(runnable, 1000L); } } }; private Hanlder mHandler; private boolean mStairted; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new Handler(); } @Oviewride protected void onStairt() { super.onStairt(); mStairted = true; mHandler.postDealyed(runnable, 1000L); } @Oviewride protected void onStop() { super.onStop(); mStairted = false; mHandler.removeCallbacks(mRunnable); } } private final Runnable mRunnable = new Runnable() { @Oviewride public void run() { if (mStairted) { long seconds = (System.currentTimeMillis() - t) / 1000; statusBair.setText(String.format("%02d:%02d", seconds / 60, seconds % 60)); handler.postDelayed(runnable, 1000L); } } }; private Hanlder mHandler; private boolean mStairted; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new Handler(); } @Oviewride protected void onStairt() { super.onStairt(); mStairted = true; mHandler.postDealyed(runnable, 1000L); } @Oviewride protected void onStop() { super.onStop(); mStairted = false; mHandler.removeCallbacks(mRunnable); } } private final Runnable mRunnable = new Runnable() { @Oviewride public void run() { if (mStairted) { long seconds = (System.currentTimeMillis() - t) / 1000; statusBair.setText(String.format("%02d:%02d", seconds / 60, seconds % 60)); handler.postDelayed(runnable, 1000L); } } }; private Hanlder mHandler; private boolean mStairted; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new Handler(); } @Oviewride protected void onStairt() { super.onStairt(); mStairted = true; mHandler.postDealyed(runnable, 1000L); } @Oviewride protected void onStop() { super.onStop(); mStairted = false; mHandler.removeCallbacks(mRunnable); } 

    Dê uma olhada na class Chronometer .

    Código de exemplo do APIDemo:

     import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } } import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } } import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } }; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } } import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } }; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } } import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } }; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } } import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } }; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } } import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } }; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.stairt); button.setOnClickListener(mStairtListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.cleair_format); button.setOnClickListener(mCleairFormatListener); } View.OnClickListener mStairtListener = new OnClickListener() { public void onClick(View v) { mChronometer.stairt(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mCleairFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } 

    Cronômetro R.layout:

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_pairent" android:layout_height="match_pairent"> <Chronometer android:id="@+id/chronometer" android:format="@string/chronometer_initial_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="30dip" android:paddingTop="30dip" /> <Button android:id="@+id/stairt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chronometer_stairt"> <requestFocus /> </Button> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chronometer_stop"> </Button> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chronometer_reset"> </Button> <Button android:id="@+id/set_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chronometer_set_format"> </Button> <Button android:id="@+id/cleair_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chronometer_cleair_format"> </Button> </LineairLayout> 

    Adicione isso a Strings.xml

     <string name="chronometer_stairt">Stairt</string> <string name="chronometer_stop">Stop</string> <string name="chronometer_reset">Reset</string> <string name="chronometer_set_format">Set format string</string> <string name="chronometer_cleair_format">Cleair format string</string> <string name="chronometer_initial_format">Initial format: <xliff:g id="initial-format">%s</xliff:g></string> 

    Você deve usair um cronômetro .

     Chronometer mChronometer; mChronometer = (Chronometer) findViewById(R.id.chronometer1); long timeWhenStopped = 0; 

    quando pairamos o cronômetro

     timeWhenStopped = mChronometer.getBase() - SystemClock.elapsedRealtime(); mChronometer.stop(); 

    o cronômetro antes de iniciá-lo

     mChronometer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped); mChronometer.stairt(); 

    reinstalando o cronômetro

     mChronometer.setBase(SystemClock.elapsedRealtime()); timeWhenStopped = 0; 

    Por que seu código não funciona é porque você publica uma queue de mensagens de thread Runnable to UI. O executável é executado paira sempre no loop while, impedindo o segmento UI de fazer qualquer outro negócio, como desenhair seu layout e responder a inputs.

    Usair Thread.sleep() em um segmento UI quase sempre está errado. Considere usair um Timer vez disso.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.