Android實(shí)現(xiàn)的秒表計(jì)時(shí)器示例
本文實(shí)例講述了Android實(shí)現(xiàn)的秒表計(jì)時(shí)器。分享給大家供大家參考,具體如下:
package com.liu.time;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class MyTime extends Activity {
private long mlCount = 0;
private long mlTimerUnit = 100;
private TextView tvTime;
private ImageButton btnStartPause;
private ImageButton btnStop;
private Timer timer = null;
private TimerTask task = null;
private Handler handler = null;
private Message msg = null;
private boolean bIsRunningFlg = false;
private static final String MYTIMER_TAG = "MYTIMER_LOG";
// menu item
private static final int SETTING_TIMER_UNIT_ID = Menu.FIRST;
private static final int ABOUT_ID = Menu.FIRST + 1;
private static final int EXIT_ID = Menu.FIRST + 2;
private static final int SETTING_SECOND_ID = Menu.FIRST + 101;
private static final int SETTING_100MILLISECOND_ID = Menu.FIRST + 102;
// Setting timer unit flag
private int settingTimerUnitFlg = SETTING_100MILLISECOND_ID;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvTime = (TextView) findViewById(R.id.tvTime);
btnStartPause = (ImageButton) findViewById(R.id.btnStartPaunse);
btnStop = (ImageButton) findViewById(R.id.btnStop);
SharedPreferences sharedPreferences = getSharedPreferences(
"mytimer_unit", Context.MODE_PRIVATE);
// getString()第二個(gè)參數(shù)為缺省值,如果preference中不存在該key,將返回缺省值
mlTimerUnit = sharedPreferences.getLong("time_unit", 100);
Log.i(MYTIMER_TAG, "mlTimerUnit = " + mlTimerUnit);
if (1000 == mlTimerUnit) {
// second
settingTimerUnitFlg = SETTING_SECOND_ID;
tvTime.setText(R.string.init_time_second);
} else if (100 == mlTimerUnit) {
// 100 millisecond
settingTimerUnitFlg = SETTING_100MILLISECOND_ID;
tvTime.setText(R.string.init_time_100millisecond);
}
// Handle timer message
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
mlCount++;
int totalSec = 0;
int yushu = 0;
if (SETTING_SECOND_ID == settingTimerUnitFlg) {
// second
totalSec = (int) (mlCount);
} else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
// 100 millisecond
totalSec = (int) (mlCount / 10);
yushu = (int) (mlCount % 10);
}
// Set time display
int min = (totalSec / 60);
int sec = (totalSec % 60);
try {
if (SETTING_SECOND_ID == settingTimerUnitFlg) {
// second(1000ms)
tvTime.setText(String.format("%1$02d:%2$02d", min,
sec));
} else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
// 100 millisecond
tvTime.setText(String.format("%1$02d:%2$02d:%3$d",
min, sec, yushu));
}
} catch (Exception e) {
tvTime.setText("" + min + ":" + sec + ":" + yushu);
e.printStackTrace();
Log.e("MyTimer onCreate", "Format string error.");
}
break;
default:
break;
}
super.handleMessage(msg);
}
};
btnStartPause.setOnClickListener(startPauseListener);
btnStop.setOnClickListener(stopListener);
}
// Start and pause
View.OnClickListener startPauseListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(MYTIMER_TAG, "Start/Pause is clicked.");
if (null == timer) {
if (null == task) {
task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if (null == msg) {
msg = new Message();
} else {
msg = Message.obtain();
}
msg.what = 1;
handler.sendMessage(msg);
}
};
}
timer = new Timer(true);
timer.schedule(task, mlTimerUnit, mlTimerUnit); // set timer
// duration
}
// start
if (!bIsRunningFlg) {
bIsRunningFlg = true;
// btnStartPause.setImageResource(R.drawable.pause);
} else { // pause
try {
bIsRunningFlg = false;
task.cancel();
task = null;
timer.cancel(); // Cancel timer
timer.purge();
timer = null;
handler.removeMessages(msg.what);
// btnStartPause.setImageResource(R.drawable.start);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
// Stop
View.OnClickListener stopListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(MYTIMER_TAG, "Stop is clicked.");
if (null != timer) {
task.cancel();
task = null;
timer.cancel(); // Cancel timer
timer.purge();
timer = null;
handler.removeMessages(msg.what);
}
mlCount = 0;
bIsRunningFlg = false;
// btnStartPause.setImageResource(R.drawable.start);
if (SETTING_SECOND_ID == settingTimerUnitFlg) {
// second
tvTime.setText(R.string.init_time_second);
} else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
// 100 millisecond
tvTime.setText(R.string.init_time_100millisecond);
}
}
};
// Menu
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
Log.i(MYTIMER_TAG, "Menu is created.");
// Stop timer
if (null != task) {
task.cancel();
task = null;
}
if (null != timer) {
timer.cancel(); // Cancel timer
timer.purge();
timer = null;
handler.removeMessages(msg.what);
}
bIsRunningFlg = false;
mlCount = 0;
// btnStartPause.setImageResource(R.drawable.start);
// 設(shè)置子菜單的名稱(chēng)
// SubMenu settingMenu = menu.addSubMenu(0, SETTING_TIMER_UNIT_ID, 0,
// R.string.menu_setting_timer_unit).setIcon(R.drawable.setting);
// 按對(duì)應(yīng)的名稱(chēng)增加子菜單
// Sub menus do not support item icons, or nested sub menus.
// settingMenu.add(1, SETTING_SECOND_ID, 0,
// R.string.menu_setting_second);
// settingMenu.add(1, SETTING_100MILLISECOND_ID, 1,
// R.string.menu_setting_100milisec);
// About
// menu.add(0, ABOUT_ID, 1,
// R.string.menu_about).setIcon(R.drawable.about);
// 退出
// menu.add(0, EXIT_ID, 2, R.string.menu_exit).setIcon(R.drawable.exit);
return true;
}
// Menu item
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Log.i(MYTIMER_TAG, "Menu item is selected.");
switch (item.getItemId()) {
case SETTING_TIMER_UNIT_ID:
break;
case ABOUT_ID:
// Display about dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.app_name)
.setMessage("本程序由雨夢(mèng)開(kāi)發(fā)/n聯(lián)系作者:minyugong@163.com")
.setCancelable(true)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
break;
case EXIT_ID:
finish(); // Exit application
break;
case SETTING_SECOND_ID: // 秒(1000ms)
if (SETTING_SECOND_ID != settingTimerUnitFlg) {
mlTimerUnit = 1000;
settingTimerUnitFlg = SETTING_SECOND_ID;
}
tvTime.setText(R.string.init_time_second);
break;
case SETTING_100MILLISECOND_ID: // 100毫秒
if (SETTING_100MILLISECOND_ID != settingTimerUnitFlg) {
mlTimerUnit = 100;
settingTimerUnitFlg = SETTING_100MILLISECOND_ID;
}
tvTime.setText(R.string.init_time_100millisecond);
break;
default:
Log.i(MYTIMER_TAG, "Other menu item...");
break;
}
// Save timer unit
try {
SharedPreferences sharedPreferences = getSharedPreferences(
"mytimer_unit", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();// 獲取編輯器
editor.putLong("time_unit", mlTimerUnit);
editor.commit();// 提交修改
} catch (Exception e) {
e.printStackTrace();
Log.e(MYTIMER_TAG, "save timer unit error.");
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (KeyEvent.KEYCODE_MENU == keyCode) {
super.openOptionsMenu(); // 調(diào)用這個(gè),就可以彈出菜單
Log.i(MYTIMER_TAG, "Menu key is clicked.");
// Stop timer
if (null != task) {
task.cancel();
task = null;
}
if (null != timer) {
timer.cancel(); // Cancel timer
timer.purge();
timer = null;
handler.removeMessages(msg.what);
}
bIsRunningFlg = false;
mlCount = 0;
// btnStartPause.setImageResource(R.drawable.start);
return true;
}
return super.onKeyDown(keyCode, event);
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android日期與時(shí)間操作技巧總結(jié)》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
android計(jì)算pad或手機(jī)的分辨率/像素/密度/屏幕尺寸/DPI值的方法
本文將介紹手機(jī)布局/界面設(shè)計(jì)/分辨率/密度相關(guān),接下來(lái)介紹android計(jì)算pad或手機(jī)的分辨率像素等等的方法,感興趣的朋友可以了解下,希望本文可以幫助你2013-01-01
android自動(dòng)化測(cè)試知識(shí)點(diǎn)總結(jié)
在本文里小編給大家分享了關(guān)于android自動(dòng)化測(cè)試入門(mén)的相關(guān)知識(shí)點(diǎn),需要的朋友們跟著參考下吧。2019-06-06
Android?IntentFilter的匹配規(guī)則示例詳解
這篇文章主要為大家介紹了Android?IntentFilter的匹配規(guī)則示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
深入解析Android中的setContentView加載布局原理
在日常開(kāi)發(fā)Android中setContentView是必不可少的一部分,下面這篇文章主要給大家介紹了關(guān)于Android中setContentView的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09
android中使用Activity實(shí)現(xiàn)監(jiān)聽(tīng)手指上下左右滑動(dòng)
這篇文章主要介紹了android中使用Activity實(shí)現(xiàn)監(jiān)聽(tīng)手指上下左右滑動(dòng),本文使用了Activity的ontouchEvent方法監(jiān)聽(tīng)手指點(diǎn)擊事件,并給出代碼實(shí)例,需要的朋友可以參考下2015-05-05
淺談AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)的分析說(shuō)明
本篇文章是對(duì)AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

