Android Timer Example Code
TimerActivity.java Code
package
android.code.timer;
import
android.app.Activity;
import
android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import
android.os.CountDownTimer;
import
android.os.Handler;
import
android.os.SystemClock;
import android.os.Bundle;
import
android.view.View;
import android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.TextView;
public class TimerActivity extends Activity {
TextView mButtonLabel;
// Counter of time since app
started ,a background task
private long mStartTime = 0L;
private TextView mTimeLabel,mTimerLabel;
// Handler to handle the message to
the timer task
private Handler mHandler = new Handler();
static final int UPDATE_INTERVAL = 1000;
String timerStop1;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTimeLabel = (TextView)
findViewById(R.id.countDownTv);
mButtonLabel = (TextView)
findViewById(R.id.countDown);
mTimerLabel = (TextView)
findViewById(R.id.textTimer);
mButtonLabel = (TextView)
findViewById(R.id.countDown);
Button countDownButton = (Button)
findViewById(R.id.countDown);
countDownButton.setOnClickListener(new
View.OnClickListener() {
public void onClick(View
view){
CountDownTimer
timer1 = new CountDownTimer(90000,1000){
@Override
public void onFinish() {
mTimeLabel.setText("Done!");
}
@Override
public void onTick(long
millisUntilFinished) {
int seconds = (int)
(millisUntilFinished / 1000);
int minutes =
seconds / 60;
seconds
= seconds % 60;
mTimeLabel.setText("" + minutes + ":"
+ String.format("%02d", seconds));
}
}.start();
}
});
Button timerStartButton = (Button)
findViewById(R.id.btnTimer);
timerStartButton.setOnClickListener(new
View.OnClickListener() {
public void onClick(View
view){
if(mStartTime == 0L){
mStartTime = SystemClock.uptimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
}
});
Button timerStopButton = (Button)
findViewById(R.id.btnTimerStop);
timerStopButton.setOnClickListener(new
View.OnClickListener() {
public void onClick(View
view){
mHandler.removeCallbacks(mUpdateTimeTask);
mTimerLabel.setText(timerStop1);
mStartTime = 0L;
}
});
} // OnCreate
private Runnable mUpdateTimeTask = new Runnable(){
public void run() {
final long start = mStartTime;
long millis =
SystemClock.uptimeMillis()- start;
int seconds = (int) (millis /
1000);
int minutes =
seconds / 60;
seconds = seconds % 60;
mTimerLabel.setText("" + minutes + ":"
+ String.format("%02d", seconds));
timerStop1 = minutes + ":"
+ String.format("%02d", seconds);
mHandler.postDelayed(this, 200);
}
};
/*
@Override
protected void onPause() {
mHandler.removeCallbacks(mUpdateTimeTask);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
mHandler.postDelayed(mUpdateTimeTask,
100);
}
*/
} //
class
main.xml Code
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#5f0101"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Timer
Counter"
android:textColor="#00FFFF"
android:textStyle="bold"
android:textSize="30dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#464444" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="5dp"
android:text="Time Left "
android:textColor="#FFFF00"
android:textSize="20dp" />
<TextView
android:id="@+id/countDownTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="1.30"
android:textColor="#00FF00"
android:textSize="30dp" />
<Button
android:id="@+id/countDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="Count Down Timer" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:background="#464444"
android:orientation="vertical" >
<TextView
android:id="@+id/textTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="0.00"
android:textColor="#FFFFFF"
android:textSize="70dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal" >
<Button
android:id="@+id/btnTimer"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Timer Start" />
<Button
android:id="@+id/btnTimerStop"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Timer Stop" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Download Android Timer Example Code
More Info
Android Alram -->
Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android
สอนเขียน Android สอนเขียนโปรแกรม Android
http://androidcontrol.blogspot.com/2012/01/beginning-android-training-android.html
ติดต่อ amphancm@gmail.com
ติดต่อ amphancm@gmail.com