在Android中使用Timer,并创建一个应用程序
来源:互联网 发布:mac文件夹无故消失 编辑:程序博客网 时间:2024/04/18 11:01
都知道java中有一个Timer:java.util.Timer;
Android中也可使用。下面来看看源代码:
public class TimerActivity extends Activity {long k = 0;long wait = 0;long length = 0;boolean isstart = false;MediaPlayer mp;Timer timer; EditText txtWait; EditText txtLength; Button btnStart; Handler HANDLER = new Handler() { @Override public void handleMessage(Message msg) { Bundle bundle=msg.getData(); switch(bundle.getInt("timer")) { case 0x0000001: Stop(); break; } super.handleMessage(msg); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtWait = (EditText) findViewById(R.id.txtWait); wait = Integer.parseInt(txtWait.getText().toString()); txtLength = (EditText) findViewById(R.id.txtLength); length = Integer.parseInt(txtLength.getText().toString()); btnStart = (Button) findViewById(R.id.btnStart); mp = MediaPlayer.create(this, R.raw.alarm);timer = new Timer(); btnStart.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(!isstart) {Start();} else {isstart = false; k = 0;//省略Stop();timer.cancel();}}}); } void Start(){timer.purge();timer = new Timer();timer.schedule(new MyTask(), wait*1000, 1000);isstart = true;//省略 } void Stop(){//省略 } class MyTask extends TimerTask {@Overridepublic void run() {if(k==wait*1000) {//省略}if(k==(wait+length)*1000) {isstart = false; k = 0;timer.cancel();Message msg = HANDLER.obtainMessage();Bundle bundle = new Bundle();bundle.putInt("timer", 0x0000001);msg.setData(bundle);HANDLER.sendMessage(msg);timer.cancel();//省略}k+=1000;} }}
main.xml就不解释了。
首先在‘省略’里面写你自己的一些代码,
我在这里使用了Handler,原因待会揭晓。
Handler中处理了Message,然后处理了Message,注意,是在main线程中处理的。
onCreate方法里,我们为btnStart添加了onClickListener,处理Timer,点击以后,Timer开始执行,再点一次,Timer停止了。
然后在Start方法里,为timer设置了TimerTask:timer.schedule(new MyTask(), wait*1000, 1000); wait*1000是Timer等待时间,以毫秒为单位, 第二个1000是指Timer的执行周期,为1秒。
MyTask 中有一个run方法,是上面的每1秒执行一次,我们的程序是计时工具,在if(k==(wait+length)*1000)中,我们处理了timer.cencel()的一些事情,我们发送了一个Message给Activity线程中的Handler,是为了让Activity处理Timer结束的一些消息,比如修改Button的Text,只能在Activity中处理,否则会报错:跨线程的错误,由此我们可以得知,TiemrTask是在新的线程中执行的。
其实,我们可以单独使用Handler进行计时的操作:
Runnable r = new Runnable(){ public void run() { //刚才在TimerTask中的内容 HANDLER.postDelayed(r, 1000); }};HANDLER.postDelayed(r, 1000);//这与刚才的timer.schedule(new TimerTask(), wait, period)相似,wait我们可以在HANDLER第一次postDelayed中将Delayed设置为wait,再在Runnable中的HANDLER.postDelayed中设置Delayed为length.(wait与length的含义参考TimerActivity的源代码)这样,我们也可以实现Timer,并且在同一线程中。要是想在不同线程中,可以使用Timer。
要cencel这个HANDLER消息队列,我们在Runnable里的run方法中的HANDLER.postDelayed()加一个if就可以了
- 在Android中使用Timer,并创建一个应用程序
- 创建并使用一个Timer对象
- Flex开发中创建并加载一个子应用程序
- vs2010中创建DLL文件,并在另外一个工程中使用测试或使用它
- 在一个窗口应用程序中创建一个console窗口
- 在Maximo7中如何使用日志记录应用程序创建一个日志文件?
- 在VMware Workstation中创建一个虚拟软盘,并在Windows XP上使用
- 在程序中调用另外一个应用程序并传递参数
- 在WebService中使用Timer
- 在WebService中使用Timer
- Android学习笔记----在一个应用程序中启动另一个应用程序
- Android在一个应用程序中启动另一个应用程序
- 使用Cocos Studio1.6创建一个Button并在Cocos2d-x 2.2.6中加载
- Xamarin.Android 使用timer 并更改UI
- Android个人学习笔记-使用myeclipse快速创建webservice并在Android中调用
- Android个人学习笔记-使用myeclipse快速创建webservice并在Android中调用(续)
- 在 Android 应用程序中使用 Internet 数据
- 在 Android 应用程序中使用 Internet 数据
- 转载只为记录经典之<linux驱动简介 >
- C#的OpenFileDialog和SaveFileDialog的常见用法
- oracle字符集
- Android常见的专业名词及术语解释
- [翻译]《The New C》——几篇介绍C99特性的文章 2010年4月30日更新
- 在Android中使用Timer,并创建一个应用程序
- JAVA程序员必看的15本书
- Oracle触发器
- 软件开发工具推荐 :Gow
- VC++ 反汇编定位程序崩溃位置
- 网页中的编码问题,编码转换问题,以及file_get_contents
- json解析类
- 字符串全攻略
- 使用 /proc 文件系统来访问 Linux 内核的内容