Pages

Subscribe:

Wednesday, February 2, 2011

Android - Timer Sample Code


Screen


Layout

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/btnStartTimer"
android:layout_width="77px"
android:layout_height="wrap_content"
android:text="Start"
android:layout_x="53px"
android:layout_y="156px"
>
</Button>
<TextView
android:id="@+id/myLabel"
android:layout_width="292px"
android:layout_height="27px"
android:text="TextView"
android:gravity="center"
android:layout_x="15px"
android:layout_y="55px"
>
</TextView>
<Button
android:id="@+id/btnStopTimer"
android:layout_width="74px"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_x="175px"
android:layout_y="155px"
>
</Button>
</AbsoluteLayout>

Java Code


package com.mytimer;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TimerSample extends Activity {

Handler mHandler = new Handler();

Button btnStartTimer;
Button btnStopTimer;

TextView txtMessage;

long mStartTime;

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

Log.i("TIMER_SAMPLE", "Creating objects for buttons");

btnStartTimer = (Button) findViewById(R.id.btnStartTimer);
btnStopTimer = (Button) findViewById(R.id.btnStopTimer);

Log.i("TIMER_SAMPLE", "Creating objects for textview");

txtMessage = (TextView) findViewById(R.id.myLabel);

Log.i("TIMER_SAMPLE", "Binding listeners for buttons");

btnStartTimer.setOnClickListener((OnClickListener) new startTimer());
btnStopTimer.setOnClickListener((OnClickListener) new stopTimer());

}

class startTimer implements Button.OnClickListener {

@Override
public void onClick(View v) {
Log.i("TIMER_SAMPLE", "Initialising timer");
if (mStartTime == 0L) {
Log.i("TIMER_SAMPLE", "Starting timer");
mStartTime = System.currentTimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 1000);
txtMessage.setText(getDateTime(mStartTime));
}
Log.i("TIMER_SAMPLE", "Timer started");
}

}

private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long timeInMillis = System.currentTimeMillis();
Log.i("TIMER_SAMPLE", "Timer updated " + getDateTime(timeInMillis));
txtMessage.setText(getDateTime(timeInMillis));
mHandler.postDelayed(this, 1000);
}
};
private String getDateTime(Long ms) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ms);
java.util.Date date = cal.getTime();
return date.toString();
}

class stopTimer implements Button.OnClickListener {

@Override
public void onClick(View v) {
Log.i("TIMER_SAMPLE", "Stopping timer");
mHandler.removeCallbacks(mUpdateTimeTask);

}

}

}

0 comments:

Post a Comment