Android多功能时钟开发案例(实战篇)
上一篇为大家介绍的是android多功能时钟开发基础内容,大家可以回顾一下,android多功能时钟开发案例(基础篇)
接下来进入实战,快点来学习吧。
一、时钟
在布局文件中我们看到,界面上只有一个textview,这个textview的作用就是显示一个系统的当前时间,同时这个时间还是一秒一秒跳的,要实现一秒一秒的跳就需要我们每隔一秒就要刷新一下,同时我们这里还考虑了切换到另一个tab的时候,这个时间就不跳动了,这样就会减少这个对系统的占用,考虑到了这点我们在这里用到了handler,通过handler发送的msg.what 来判断是否要刷新时间。
public class timeview extends linearlayout { private textview tvtime; public timeview(context context) { super(context); } public timeview(context context, attributeset attrs) { super(context, attrs); } public timeview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } @override protected void onfinishinflate() { super.onfinishinflate(); tvtime = (textview) findviewbyid(r.id.tvtime); //tvtime.settext("hello"); timehandler.sendemptymessage(0); } @override protected void onvisibilitychanged(view changedview, int visibility) { super.onvisibilitychanged(changedview, visibility); //当再次切换到这个tab时我们就再发送一次这个消息,否者就把所有的消息移除掉 if (visibility == view.visible) { timehandler.sendemptymessage(0); }else{ timehandler.removemessages(0); } } private void refreshtime(){ //获取当前的时间 calendar c = calendar.getinstance(); tvtime.settext(string.format("%d:%d:%d", c.get(calendar.hour_of_day),c.get(calendar.minute),c.get(calendar.second))); } private handler timehandler = new handler(){ public void handlemessage(android.os.message msg) { refreshtime(); //处于当前tab的时候给自己发送信息,可以刷新 if (getvisibility() == view.visible) { //1秒钟后再次执行以下sendemptymessage,what参数用于区分不同的message timehandler.sendemptymessagedelayed(0, 1000); } }; }; }
其实这里的handler可以用timer来完成亦可以达到同样的效果。
在这里要提一下的是onfinishinflate(),这在我们自定义布局的时候一定要用到的,解释以及例子在之后上传的知识点中同样有,看看那个就可以了。
二、闹钟
从第二个布局中我们可以看到,我们在这里用到了一个listview,这是用来存储我们添加的闹钟的,既然这里用到了listview,那么我们接着就会想到要给这个listview一个适配器adapter,因此我们会在这里创建这么一个适配器,
private arrayadapter<alarmdata> adapter;
看到这里可能又会有疑问了,alarmdata这是个什么东西?有这么一个数据类型吗??其实这里我们自定义了一个数据类型,用来专门存储一下创建的闹钟时间。我们来看一下自定义的数据类型代码吧!
// 自定义数据类型 private static class alarmdata { private long time = 0; private calendar date; private string timelabel = ""; public alarmdata(long time) { this.time = time; date = calendar.getinstance(); date.settimeinmillis(time); timelabel = string.format("%d月%d日 %d:%d", date.get(calendar.month) + 1, date.get(calendar.day_of_month), date.get(calendar.hour_of_day), date.get(calendar.minute)); } public long gettime() { return time; } public string gettimelabel() { return timelabel; } public int getid() { return (int) (gettime() / 1000 / 60); } @override public string tostring() { return gettimelabel(); } }
这个数据类型的代码其实还是很容易明白的,不多讲了。
当我们到这里的时候,我们其实还没有真正的完成,假如我们的代码已经写好了,并且可以运行了我们运行一次后,并且添加了n个闹钟,当我们退出程序,再次打开就会发现,我们之前创建的闹钟都没了,原因是我们虽然把数据临时的保存在了listview中,但是我们并没有长时间的保存,因此我们接着就来讲讲长久的保存这些闹钟数据。
private void savealarmlist() { editor editor = getcontext().getsharedpreferences( alarmview.class.getname(), context.mode_private).edit(); stringbuffer sb = new stringbuffer(); for (int i = 0; i < adapter.getcount(); i++) { sb.append(adapter.getitem(i).gettime()).append(","); } if (sb.length() > 1) { string content = sb.tostring().substring(0, sb.length() - 1); editor.putstring(key_alarm_list, content); system.out.println(content); } else { editor.putstring(key_alarm_list, null); } editor.commit(); }
有了保存,我们当然的会想到读取
private void readsavealarmlist() { sharedpreferences sp = getcontext().getsharedpreferences( alarmview.class.getname(), context.mode_private); string content = sp.getstring(key_alarm_list, null); if (content != null) { string[] timestrings = content.split(","); for (string string : timestrings) { adapter.add(new alarmdata(long.parselong(string))); } } }
上面的一些陌生的类型在之后的知识点中可以查看。
接着我们来看看最关键的就是添加闹钟
private void addalarm() { calendar c = calendar.getinstance(); new tpdiolog(getcontext(), new timepickerdialog.ontimesetlistener() { @override public void ontimeset(timepicker view, int hourofday, int minute) { calendar calendar = calendar.getinstance(); calendar.set(calendar.hour_of_day, hourofday); calendar.set(calendar.minute, minute); calendar.set(calendar.second, 0); calendar.set(calendar.millisecond, 0); calendar currenttime = calendar.getinstance(); if (currenttime.gettimeinmillis() >= calendar.gettimeinmillis()) { calendar.settimeinmillis(calendar.gettimeinmillis() + 24 * 60 * 60 * 1000); } alarmdata ad = new alarmdata(calendar.gettimeinmillis()); adapter.add(ad); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, ad.gettime(), 5 * 60 * 1000, pendingintent .getbroadcast(getcontext(), ad.getid(), new intent(getcontext(), alarmreceiver.class), 0)); savealarmlist(); } }, c.get(calendar.hour_of_day), c.get(calendar.minute), true).show(); }
这里我们可以看到tpdiolog这个,当你自己尝试过后可能也会遇到同样的问题,那就是当你通过timepickerdialog这个系统的时间选择控件的时候,点击确定后,会创建两条记录,这是因为我们点击确定后会调用该事件监听器的时间,在关闭这个dialog的时候也会调用一次,所以我们在这里自己重写了一下该类的方法
tpdiolog.class
public class tpdiolog extends timepickerdialog { public tpdiolog(context context, ontimesetlistener callback, int hourofday, int minute, boolean is24hourview) { super(context, callback, hourofday, minute, is24hourview); } //重写该方法是为了避免调用两次ontimeset @override protected void onstop() { //super.onstop(); } }
在之前的代码中我们还看到了一个alarmmanager这一对象,这是我们为了调用系统的闹钟服务创建的实例,我们也因此而创建了一个alarmreceiver.class
public class alarmreceiver extends broadcastreceiver { @override public void onreceive(context context, intent arg1) { system.out.println("闹钟执行了!"); alarmmanager am=(alarmmanager) context.getsystemservice(context.alarm_service); am.cancel(pendingintent.getbroadcast(context, getresultcode(), new intent(context, alarmreceiver.class), 0)); intent i =new intent(context,playalarmaty.class); //设置intent的启动模式 i.addflags(intent.flag_activity_new_task); context.startactivity(i); } }
一些小的地方讲好了,最后把alarmview.class的完整代码贴上。
package com.example.clock; import java.util.calendar; import java.util.date; import java.util.iterator; import android.app.alarmmanager; import android.app.alertdialog; import android.app.pendingintent; import android.app.timepickerdialog; import android.app.timepickerdialog.ontimesetlistener; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.util.attributeset; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemlongclicklistener; import android.widget.arrayadapter; import android.widget.button; import android.widget.linearlayout; import android.widget.listview; import android.widget.switch; import android.widget.timepicker; public class alarmview extends linearlayout { private button btnaddalarm; private listview lvlistalarm; private arrayadapter<alarmdata> adapter; private alarmmanager alarmmanager; public alarmview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(); } public alarmview(context context, attributeset attrs) { super(context, attrs); init(); } public alarmview(context context) { super(context); init(); } private void init() { alarmmanager = (alarmmanager) getcontext().getsystemservice( context.alarm_service); } @override protected void onfinishinflate() { super.onfinishinflate(); btnaddalarm = (button) findviewbyid(r.id.btnaddalarm); lvlistalarm = (listview) findviewbyid(r.id.lvlistalarm); adapter = new arrayadapter<alarmdata>(getcontext(), android.r.layout.simple_list_item_1); lvlistalarm.setadapter(adapter); readsavealarmlist(); // adapter.add(new alarmdata(system.currenttimemillis())); btnaddalarm.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { addalarm(); } }); // 长按某项删除 lvlistalarm.setonitemlongclicklistener(new onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> arg0, view arg1, final int position, long arg3) { new alertdialog.builder(getcontext()) .settitle("操作选项") .setitems(new charsequence[] { "删除", "删除1" }, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { switch (which) { case 0: deletealarm(position); break; default: break; } } }).setnegativebutton("取消", null).show(); return true; } }); } private void deletealarm(int position) { alarmdata ad = adapter.getitem(position); adapter.remove(ad); savealarmlist(); alarmmanager.cancel(pendingintent.getbroadcast(getcontext(), ad.getid(), new intent(getcontext(), alarmreceiver.class), 0)); } private void addalarm() { calendar c = calendar.getinstance(); new tpdiolog(getcontext(), new timepickerdialog.ontimesetlistener() { @override public void ontimeset(timepicker view, int hourofday, int minute) { calendar calendar = calendar.getinstance(); calendar.set(calendar.hour_of_day, hourofday); calendar.set(calendar.minute, minute); calendar.set(calendar.second, 0); calendar.set(calendar.millisecond, 0); calendar currenttime = calendar.getinstance(); if (currenttime.gettimeinmillis() >= calendar.gettimeinmillis()) { calendar.settimeinmillis(calendar.gettimeinmillis() + 24 * 60 * 60 * 1000); } alarmdata ad = new alarmdata(calendar.gettimeinmillis()); adapter.add(ad); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, ad.gettime(), 5 * 60 * 1000, pendingintent .getbroadcast(getcontext(), ad.getid(), new intent(getcontext(), alarmreceiver.class), 0)); savealarmlist(); } }, c.get(calendar.hour_of_day), c.get(calendar.minute), true).show(); } private static final string key_alarm_list = "alarmlist"; private void savealarmlist() { editor editor = getcontext().getsharedpreferences( alarmview.class.getname(), context.mode_private).edit(); stringbuffer sb = new stringbuffer(); for (int i = 0; i < adapter.getcount(); i++) { sb.append(adapter.getitem(i).gettime()).append(","); } if (sb.length() > 1) { string content = sb.tostring().substring(0, sb.length() - 1); editor.putstring(key_alarm_list, content); system.out.println(content); } else { editor.putstring(key_alarm_list, null); } editor.commit(); } private void readsavealarmlist() { sharedpreferences sp = getcontext().getsharedpreferences( alarmview.class.getname(), context.mode_private); string content = sp.getstring(key_alarm_list, null); if (content != null) { string[] timestrings = content.split(","); for (string string : timestrings) { adapter.add(new alarmdata(long.parselong(string))); } } } // 自定义数据类型 private static class alarmdata { private long time = 0; private calendar date; private string timelabel = ""; public alarmdata(long time) { this.time = time; date = calendar.getinstance(); date.settimeinmillis(time); timelabel = string.format("%d月%d日 %d:%d", date.get(calendar.month) + 1, date.get(calendar.day_of_month), date.get(calendar.hour_of_day), date.get(calendar.minute)); } public long gettime() { return time; } public string gettimelabel() { return timelabel; } public int getid() { return (int) (gettime() / 1000 / 60); } @override public string tostring() { return gettimelabel(); } } }
三、计时器
计时器的主要功能就是你先设定一个时间,然后点击开始,时间就会一秒一秒的减少,在这里我么主要用到了timer这个系统的计时器,这代码中没有上面难懂的地方,有些地方已经给上注释了,所以直接贴代码,可能有些人会不知道timer怎么用,之后的知识点中都会有提到。
package com.example.clock; import java.util.timer; import java.util.timertask; import android.r.integer; import android.app.alertdialog; import android.content.context; import android.os.handler; import android.text.editable; import android.text.textutils; import android.text.textwatcher; import android.util.attributeset; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.linearlayout; public class timerview extends linearlayout { public timerview(context context, attributeset attrs) { super(context, attrs); } public timerview(context context) { super(context); } @override protected void onfinishinflate() { super.onfinishinflate(); btnstart = (button) findviewbyid(r.id.btnstart); btnpause = (button) findviewbyid(r.id.btnpause); btnresume = (button) findviewbyid(r.id.btnresume); btnreset = (button) findviewbyid(r.id.btnreset); btnstart.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { starttimer(); btnstart.setvisibility(view.gone); btnpause.setvisibility(view.visible); btnreset.setvisibility(view.visible); } }); btnpause.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { stoptimer(); btnpause.setvisibility(view.gone); btnresume.setvisibility(view.visible); } }); btnresume.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { starttimer(); btnpause.setvisibility(view.visible); btnresume.setvisibility(view.gone); } }); btnreset.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { stoptimer(); ethour.settext("00"); etmin.settext("00"); etsec.settext("00"); btnreset.setvisibility(view.gone); btnresume.setvisibility(view.gone); btnpause.setvisibility(view.gone); btnstart.setvisibility(view.visible); } }); ethour = (edittext) findviewbyid(r.id.ethour); etmin = (edittext) findviewbyid(r.id.etmin); etsec = (edittext) findviewbyid(r.id.etsec); ethour.settext("00"); ethour.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { /* * 这个方法是在text改变过程中触发调用的, 它的意思就是说在原有的文本s中, * 从start开始的count个字符替换长度为before的旧文本, * 注意这里没有将要之类的字眼,也就是说一句执行了替换动作。 */ if (!textutils.isempty(s)) { int value = integer.parseint(s.tostring()); if (value > 59) { ethour.settext("59"); } else if (value < 0) { ethour.settext("00"); } } checktoenablebtnstart(); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void aftertextchanged(editable s) { } }); etmin.settext("00"); etmin.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { if (!textutils.isempty(s)) { int value = integer.parseint(s.tostring()); if (value > 59) { etmin.settext("59"); } else if (value < 0) { etmin.settext("00"); } } checktoenablebtnstart(); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void aftertextchanged(editable s) { } }); etsec.settext("00"); etsec.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { if (!textutils.isempty(s)) { int value = integer.parseint(s.tostring()); if (value > 59) { etsec.settext("59"); } else if (value < 0) { etsec.settext("00"); } } checktoenablebtnstart(); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void aftertextchanged(editable s) { } }); btnstart.setvisibility(view.visible); btnstart.setenabled(false); btnpause.setvisibility(view.gone); btnresume.setvisibility(view.gone); btnreset.setvisibility(view.gone); } private void checktoenablebtnstart() { btnstart.setenabled((!textutils.isempty(ethour.gettext()) && integer .parseint(ethour.gettext().tostring()) > 0) || (!textutils.isempty(etmin.gettext()) && integer .parseint(etmin.gettext().tostring()) > 0) || (!textutils.isempty(etsec.gettext()) && integer .parseint(etsec.gettext().tostring()) > 0)); } private void starttimer() { if (timertask == null) { alltimecount = integer.parseint(ethour.gettext().tostring()) * 60 * 60 + integer.parseint(etmin.gettext().tostring()) * 60 + integer.parseint(etsec.gettext().tostring()); timertask = new timertask() { @override public void run() { alltimecount--; handle.sendemptymessage(msg_what_time_tick); if (alltimecount <= 0) { handle.sendemptymessage(msg_what_time_is_up); stoptimer(); } } }; timer.schedule(timertask, 1000, 1000); } } private void stoptimer(){ if (timertask!=null) { timertask.cancel(); timertask=null; } } private handler handle = new handler(){ public void handlemessage(android.os.message msg) { switch (msg.what) { case msg_what_time_tick: int hour = alltimecount/60/60; int min = (alltimecount/60)%60; int sec = alltimecount%60; ethour.settext(hour+""); etmin.settext(min+""); etsec.settext(sec+""); break; case msg_what_time_is_up: new alertdialog.builder(getcontext()) .settitle("time is up!") .setmessage("time is up!") .setnegativebutton("cancle", null).show(); btnreset.setvisibility(view.gone); btnresume.setvisibility(view.gone); btnpause.setvisibility(view.gone); btnstart.setvisibility(view.visible); break; default: break; } }; }; private static final int msg_what_time_is_up = 1; private static final int msg_what_time_tick = 2; private int alltimecount = 0; private timer timer = new timer(); private timertask timertask = null; private button btnstart, btnpause, btnresume, btnreset; private edittext ethour, etmin, etsec; }
四、秒表
最后的秒表相信大家都不陌生,用到的知识正好之前的三个都有讲到,只要明白前三个后,这个就不难理解了。
package com.example.clock; import java.util.arraylist; import java.util.timer; import java.util.timertask; import android.content.context; import android.os.handler; import android.util.attributeset; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.linearlayout; import android.widget.listview; import android.widget.textview; public class stopwatchview extends linearlayout { private textview tvhour,tvmin,tvsec,tvmsec; private button btnstart,btnpause,btnresume,btnreset,btnlap; private listview lvtimelist; private arrayadapter<string> adapter; public stopwatchview(context context, attributeset attrs) { super(context, attrs); } @override protected void onfinishinflate() { super.onfinishinflate(); tvhour = (textview) findviewbyid(r.id.timehour); tvhour.settext("0"); tvmin = (textview) findviewbyid(r.id.timemin); tvmin.settext("0"); tvsec = (textview) findviewbyid(r.id.timesec); tvsec.settext("0"); tvmsec = (textview) findviewbyid(r.id.timemsec); tvmsec.settext("0"); btnstart = (button) findviewbyid(r.id.btnswstart); btnpause = (button) findviewbyid(r.id.btnswpause); btnresume = (button) findviewbyid(r.id.btnswresume); btnlap = (button) findviewbyid(r.id.btnswlap); btnreset = (button) findviewbyid(r.id.btnswreset); btnstart.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { starttimer(); btnstart.setvisibility(view.gone); btnpause.setvisibility(view.visible); btnlap.setvisibility(view.visible); } }); btnpause.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { stoptimer(); btnpause.setvisibility(view.gone); btnresume.setvisibility(view.visible); btnlap.setvisibility(view.gone); btnreset.setvisibility(view.visible); } }); btnresume.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { starttimer(); btnresume.setvisibility(view.gone); btnpause.setvisibility(view.visible); btnlap.setvisibility(view.visible); btnreset.setvisibility(view.gone); } }); btnreset.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { stoptimer(); tenmsecs = 0; adapter.clear(); btnreset.setvisibility(view.gone); btnlap.setvisibility(view.gone); btnpause.setvisibility(view.gone); btnresume.setvisibility(view.gone); btnstart.setvisibility(view.visible); } }); btnlap.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { adapter.insert(string.format("%d:%d:%d.%d", tenmsecs/100/60/60,tenmsecs/100/60%60,tenmsecs/100%60,tenmsecs%100), 0); } }); btnlap.setvisibility(view.gone); btnpause.setvisibility(view.gone); btnresume.setvisibility(view.gone); btnreset.setvisibility(view.gone); lvtimelist = (listview) findviewbyid(r.id.lvwatchtime); adapter = new arrayadapter<string>(getcontext(), android.r.layout.simple_list_item_1); lvtimelist.setadapter(adapter); showtimertask = new timertask() { @override public void run() { handle.sendemptymessage(msg_what_show_time); } }; timer.schedule(showtimertask, 200, 200); } private void starttimer(){ if (timertask == null) { timertask = new timertask() { @override public void run() { tenmsecs++; } }; timer.schedule(timertask, 10, 10); } } private void stoptimer(){ if (timertask != null) { timertask.cancel(); timertask = null; } } private int tenmsecs = 0; private timer timer =new timer(); private timertask timertask = null; private timertask showtimertask = null; private static final int msg_what_show_time = 1; private handler handle = new handler(){ public void handlemessage(android.os.message msg) { switch (msg.what) { case msg_what_show_time: tvhour.settext(tenmsecs/100/60/60+""); tvmin.settext(tenmsecs/100/60%60+""); tvsec.settext(tenmsecs/100%60+""); tvmsec.settext(tenmsecs%100+""); break; default: break; } }; }; public void ondestroy() { timer.cancel(); } }
到此为止,自己的第一个实战算是完成了,但是就是界面很low,这个只是把基本的功能实现了,但是在界面上没有做很大的完善,在之后的实战中会慢慢改进的。
源码下载:android多功能时钟
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android编写简单的聊天室应用
下一篇: Python生成随机密码的方法