2010-09-17 15 views
19

Quiero desarrollar una lógica de cronómetro simple en android.lógica de cronómetro

Al hacer clic en una vista de lista, se debe iniciar el temporizador y al hacer clic en el botón, el temporizador debe detenerse. ¿Alguien puede por favor guiarme? Cualquier código de ejemplo será de gran ayuda

Respuesta

33

Uso del Stopwatch Clase (Para una mayor utilización de precisión System.nanoTime())

Añadir un evento Start() y stop() en pulsaciones de botón. Necesitarás actualizar la UI así que usa una combinación de Thread/Handler.

Esto debería comenzar.

EDITAR: Agregó el código. (Buen ejercicio! :))

Utilice Refresh_Rate para configurar la frecuencia de su interfaz de usuario.

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class Main extends Activity implements OnClickListener{ 

    final int MSG_START_TIMER = 0; 
    final int MSG_STOP_TIMER = 1; 
    final int MSG_UPDATE_TIMER = 2; 

    Stopwatch timer = new Stopwatch(); 
    final int REFRESH_RATE = 100; 

    Handler mHandler = new Handler() 
    { 
     @Override 
     public void handleMessage(Message msg) { 
      super.handleMessage(msg); 
      switch (msg.what) { 
      case MSG_START_TIMER: 
       timer.start(); //start timer 
       mHandler.sendEmptyMessage(MSG_UPDATE_TIMER); 
       break; 

      case MSG_UPDATE_TIMER: 
       tvTextView.setText(""+ timer.getElapsedTime()); 
       mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); //text view is updated every second, 
       break;         //though the timer is still running 
      case MSG_STOP_TIMER: 
       mHandler.removeMessages(MSG_UPDATE_TIMER); // no more updates. 
       timer.stop();//stop timer 
       tvTextView.setText(""+ timer.getElapsedTime()); 
       break; 

      default: 
       break; 
      } 
     } 
    }; 

    TextView tvTextView; 
    Button btnStart,btnStop; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tvTextView = (TextView)findViewById(R.id.TextView01); 

     btnStart = (Button)findViewById(R.id.Button01); 
     btnStop= (Button)findViewById(R.id.Button02); 
     btnStart.setOnClickListener(this); 
     btnStop.setOnClickListener(this); 

    } 

    public void onClick(View v) { 
     if(btnStart == v) 
     { 
      mHandler.sendEmptyMessage(MSG_START_TIMER); 
     }else 
     if(btnStop == v){ 
      mHandler.sendEmptyMessage(MSG_STOP_TIMER); 
     } 
    } 
} 
+0

Perdón por continuar con la pregunta ... En realidad, estoy atascado en la actualización de la interfaz de usuario en Android. He usado esta clase en Java. Pero, ¿cómo puedo actualizar decir una etiqueta con la hora de mostrar ...? –

+0

@Rahul, publique lo que tiene, construiré sobre eso ... – st0le

+1

Tengo un diseño simple. Una etiqueta y dos botones. Al hacer clic en iniciar, se debe iniciar un temporizador y al hacer clic en detener el temporizador debe detenerse ... Eso es todo. Nada complejo. Solo necesito cómo ejecutar el temporizador y actualizar la etiqueta con el tiempo ... –

0

Buen ejemplo, por si acaso si alguien quiere que el archivo de diseño vaya con esto (aunque bastante simple).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<EditText 
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="18dp" 
    android:layout_marginTop="49dp" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/TextView01" 
    android:layout_marginTop="42dp" 
    android:layout_toRightOf="@+id/textView1" 
    android:text="Start" /> 

<Button 
    android:id="@+id/Button02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/Button01" 
    android:layout_marginTop="14dp" 
    android:layout_toRightOf="@+id/textView1" 
    android:text="Stop" /> 

Compiled on Nexus-5

12

Como st0le dio un excelente ejemplo mediante el uso de Stopwatch clase. Modifiqué un poco esta clase y le agregué algunos métodos.

/* 
Copyright (c) 2005, Corey Goldberg 

StopWatch.java is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; either version 2 of the License, or 
(at your option) any later version. 

Modified: Bilal Rabbani [email protected] (Nov 2013) 
*/ 

package bilalrabbani1.at.live.com; 

public class Stopwatch { 
private long startTime = 0; 
private boolean running = false; 
private long currentTime = 0; 

public void start() { 
    this.startTime = System.currentTimeMillis(); 
    this.running = true; 
} 

public void stop() { 
    this.running = false; 
} 

public void pause() { 
    this.running = false; 
    currentTime = System.currentTimeMillis() - startTime; 
} 
public void resume() { 
    this.running = true; 
    this.startTime = System.currentTimeMillis() - currentTime; 
} 

//elaspsed time in milliseconds 
public long getElapsedTimeMili() { 
    long elapsed = 0; 
    if (running) { 
     elapsed =((System.currentTimeMillis() - startTime)/100) % 1000 ; 
    } 
    return elapsed; 
} 

//elaspsed time in seconds 
public long getElapsedTimeSecs() { 
    long elapsed = 0; 
    if (running) { 
     elapsed = ((System.currentTimeMillis() - startTime)/1000) % 60; 
    } 
    return elapsed; 
} 

    //elaspsed time in minutes 
public long getElapsedTimeMin() { 
    long elapsed = 0; 
    if (running) { 
     elapsed = (((System.currentTimeMillis() - startTime)/1000)/60) % 60; 
    } 
    return elapsed; 
} 

//elaspsed time in hours 
public long getElapsedTimeHour() { 
    long elapsed = 0; 
    if (running) { 
     elapsed = ((((System.currentTimeMillis() - startTime)/1000)/60)/60); 
    } 
    return elapsed; 
} 

public String toString() { 
    return getElapsedTimeHour() + ":" + getElapsedTimeMin() + ":" 
      + getElapsedTimeSecs() + "." + getElapsedTimeMili(); 
} 
} 

Saludos

+1

Exactamente lo que quiero. ¡¡¡Gracias!!! –

0

basado IntentService, sin dependencias no SDK y en un solo archivo:

import android.app.Activity; 
import android.app.IntentService; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.os.Handler; 
import android.support.v4.content.LocalBroadcastManager; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class Main extends Activity { 
    static final String BROADCAST_ACTION = "com.cirosantilli.android_cheat.intent_service_text_view.BROADCAST"; 
    static final String EXTENDED_DATA_STATUS = "com.cirosantilli.android_cheat.intent_service_text_view.BROADCAST"; 
    static final String TAG = "com.cirosantilli"; 

    private int i = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.d(TAG, "onCreate"); 
     super.onCreate(savedInstanceState); 
     final LinearLayout linearLayout = new LinearLayout(this); 
     Button button; 
     final Intent intent = new Intent(Main.this, MyService.class); 

     button = new Button(this); 
     button.setText("start service"); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Log.d(TAG, "start button"); 
       Main.this.startService(intent.putExtra(Main.EXTENDED_DATA_STATUS, Main.this.i)); 
      } 
     }); 
     linearLayout.addView(button); 

     button = new Button(this); 
     button.setText("stop service"); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Log.d(TAG, "stop button"); 
       Main.this.stopService(intent); 
      } 
     }); 
     linearLayout.addView(button); 

     final TextView textView = new TextView(this); 
     textView.setText(Integer.toString(i)); 
     linearLayout.addView(textView); 
     this.setContentView(linearLayout); 

     LocalBroadcastManager.getInstance(this).registerReceiver(
       new BroadcastReceiver() { 
        @Override 
        public void onReceive(Context context, Intent intent) { 
         Main.this.i = intent.getIntExtra(Main.EXTENDED_DATA_STATUS, 0); 
         textView.setText(Integer.toString(Main.this.i)); 
        } 
       }, new IntentFilter(Main.BROADCAST_ACTION) 
     ); 
    } 

    public static class MyService extends IntentService { 
     private Handler mHandler; 
     private int i = 1; 
     private boolean done; 
     public MyService() { 
      super("MyService"); 
     } 
     @Override 
     protected void onHandleIntent(Intent intent) { 
      Log.d(TAG, "onHandleIntent"); 
      this.i = intent.getIntExtra(Main.EXTENDED_DATA_STATUS, 0); 
      this.done = false; 
      while(!done) { 
       Log.d(TAG, "while true"); 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        Thread.currentThread().interrupt(); 
       } 
       LocalBroadcastManager.getInstance(this).sendBroadcast(
         new Intent(Main.BROADCAST_ACTION) 
         .putExtra(Main.EXTENDED_DATA_STATUS, MyService.this.i)); 
       this.i++; 
      } 
     } 
     @Override 
     public void onDestroy() { 
      Log.d(TAG, "onDestroy"); 
      this.done = true; 
      super.onDestroy(); 
     } 
    } 
} 

Para única baja precisión. Podríamos obtener una mayor precisión usando System.currentTimeMillis dentro de onHandleIntent en lugar de usar el valor entero, y reducir el tiempo de inactividad para reducir la latencia.

Probado en Android 22. Plantilla estándar here.