Android实现闹钟小程序
程序员文章站
2023-11-25 16:37:52
最近写了个闹钟的程序,看到sharedpreferences在一个程序中可以共享数据,sharedpreferences是一个轻量级的键值存储机制,只可以存储基本数据类型。...
最近写了个闹钟的程序,看到sharedpreferences在一个程序中可以共享数据,sharedpreferences是一个轻量级的键值存储机制,只可以存储基本数据类型。我就拿来用用,没想到sharedpreferences太好了,真是轻量级的保存数据的好的工具,比sqlite好用多了!以后我又多了一种编程思想了,呵呵,所以现在分享给大家,特别注意这点:这个无法直接在多个程序间共享preferences数据。程序关闭再打开时间仍然保留你上次设置的时间。这就是preferences的作用!
程序欢迎界面:
点击设置闹钟界面:
点击闹钟设置中的设置后的界面:
闹钟时间到了弹出dialog:
设置重复想起闹钟后的界面:
点击返回键弹出的提示:
下面请看代码:
一、mainactivity中的代码:
package com.cn.daming; import java.util.calendar; import android.app.activity; import android.app.alarmmanager; import android.app.alertdialog; import android.app.pendingintent; import android.app.timepickerdialog; import android.content.dialoginterface; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.view.keyevent; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.timepicker; import android.widget.toast; public class mainactivity extends activity { textview settime1; textview settime2; textview settime3; button mbutton1; button mbutton2; button mbutton3; button mbutton4; button mbutton5; button mbutton6; string time1string = null; string time2string = null; string time3string = null; string defalutstring = "目前无设置"; alertdialog builder = null; calendar c=calendar.getinstance(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //取得活动的preferences对象 sharedpreferences settings = getpreferences(activity.mode_private); time1string = settings.getstring("time1", defalutstring); time2string = settings.getstring("time2", defalutstring); time3string = settings.getstring("time3", defalutstring); initbutton1(); initbutton2(); initbutton3(); initbutton4(); initbutton5(); initbutton6(); settime1.settext(time1string); settime3.settext(time2string); settime2.settext(time3string); } public void initbutton1() { settime1=(textview) findviewbyid(r.id.settime1); mbutton1=(button)findviewbyid(r.id.mbutton1); mbutton1.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { c.settimeinmillis(system.currenttimemillis()); int mhour=c.get(calendar.hour_of_day); int mminute=c.get(calendar.minute); new timepickerdialog(mainactivity.this, new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view,int hourofday, int minute) { c.settimeinmillis(system.currenttimemillis()); c.set(calendar.hour_of_day,hourofday); c.set(calendar.minute,minute); c.set(calendar.second,0); c.set(calendar.millisecond,0); intent intent = new intent(mainactivity.this, callalarm.class); pendingintent sender=pendingintent.getbroadcast( mainactivity.this,0, intent, 0); alarmmanager am; am = (alarmmanager)getsystemservice(alarm_service); am.set(alarmmanager.rtc_wakeup, c.gettimeinmillis(), sender ); string tmps=format(hourofday)+":"+format(minute); settime1.settext(tmps); //sharedpreferences保存数据,并提交 sharedpreferences time1share = getpreferences(0); sharedpreferences.editor editor = time1share.edit(); editor.putstring("time1", tmps); editor.commit(); toast.maketext(mainactivity.this,"设置大明闹钟时间为"+tmps, toast.length_short) .show(); } },mhour,mminute,true).show(); } }); } public void initbutton2() { mbutton2=(button) findviewbyid(r.id.mbutton2); mbutton2.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent intent = new intent(mainactivity.this, callalarm.class); pendingintent sender=pendingintent.getbroadcast( mainactivity.this,0, intent, 0); alarmmanager am; am =(alarmmanager)getsystemservice(alarm_service); am.cancel(sender); toast.maketext(mainactivity.this,"大明闹钟时间删除", toast.length_short).show(); settime1.settext("目前无设置"); sharedpreferences time1share = getpreferences(0); sharedpreferences.editor editor = time1share.edit(); editor.putstring("time1", "目前无设置"); editor.commit(); } }); } public void initbutton3() { settime3=(textview) findviewbyid(r.id.settime5); mbutton3=(button)findviewbyid(r.id.mbutton5); mbutton3.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { c.settimeinmillis(system.currenttimemillis()); int mhour=c.get(calendar.hour_of_day); int mminute=c.get(calendar.minute); new timepickerdialog(mainactivity.this, new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view,int hourofday, int minute) { c.settimeinmillis(system.currenttimemillis()); c.set(calendar.hour_of_day,hourofday); c.set(calendar.minute,minute); c.set(calendar.second,0); c.set(calendar.millisecond,0); intent intent = new intent(mainactivity.this, callalarm.class); pendingintent sender=pendingintent.getbroadcast( mainactivity.this,1, intent, 0); alarmmanager am; am = (alarmmanager)getsystemservice(alarm_service); am.set(alarmmanager.rtc_wakeup, c.gettimeinmillis(), sender ); string tmps=format(hourofday)+":"+format(minute); settime3.settext(tmps); //sharedpreferences保存数据,并提交 sharedpreferences time2share = getpreferences(1); sharedpreferences.editor editor = time2share.edit(); editor.putstring("time2", tmps); editor.commit(); toast.maketext(mainactivity.this,"设置大明闹钟时间为"+tmps, toast.length_short) .show(); } },mhour,mminute,true).show(); } }); } public void initbutton4() { mbutton4=(button) findviewbyid(r.id.mbutton6); mbutton4.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent intent = new intent(mainactivity.this, callalarm.class); pendingintent sender=pendingintent.getbroadcast( mainactivity.this,0, intent, 0); alarmmanager am; am =(alarmmanager)getsystemservice(alarm_service); am.cancel(sender); toast.maketext(mainactivity.this,"大明闹钟时间删除", toast.length_short).show(); settime3.settext("目前无设置"); //sharedpreferences保存数据,并提交 sharedpreferences time2share = getpreferences(1); sharedpreferences.editor editor = time2share.edit(); editor.putstring("time2", "目前无设置"); editor.commit(); } }); } public void initbutton5() { settime2=(textview) findviewbyid(r.id.settime2); layoutinflater factory = layoutinflater.from(this); final view setview = factory.inflate(r.layout.timeset,null); final timepicker tpicker=(timepicker)setview .findviewbyid(r.id.tpicker); tpicker.setis24hourview(true); final alertdialog di=new alertdialog.builder(mainactivity.this) .seticon(r.drawable.clock) .settitle("设置") .setview(setview) .setpositivebutton("确定", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { edittext ed=(edittext)setview.findviewbyid(r.id.medit); int times=integer.parseint(ed.gettext().tostring()) *1000; c.settimeinmillis(system.currenttimemillis()); c.set(calendar.hour_of_day,tpicker.getcurrenthour()); c.set(calendar.minute,tpicker.getcurrentminute()); c.set(calendar.second,0); c.set(calendar.millisecond,0); intent intent = new intent(mainactivity.this, callalarm.class); pendingintent sender = pendingintent.getbroadcast( mainactivity.this,1, intent, 0); alarmmanager am; am = (alarmmanager)getsystemservice(alarm_service); am.setrepeating(alarmmanager.rtc_wakeup, c.gettimeinmillis(),times,sender); string tmps=format(tpicker.getcurrenthour())+":"+ format(tpicker.getcurrentminute()); string substr = "设置大明闹钟时间为"+tmps+ "开始,重复间隔为"+times/1000+"秒"; settime2.settext("设置大明闹钟时间为"+tmps+ "开始,重复间隔为"+times/1000+"秒"); //sharedpreferences保存数据,并提交 sharedpreferences time3share = getpreferences(2); sharedpreferences.editor editor = time3share.edit(); editor.putstring("time3", substr); editor.commit(); toast.maketext(mainactivity.this,"设置大明闹钟为"+tmps+ "开始,重复间隔为"+times/1000+"秒", toast.length_short).show(); } }) .setnegativebutton("取消", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { } }).create(); mbutton5=(button) findviewbyid(r.id.mbutton3); mbutton5.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { c.settimeinmillis(system.currenttimemillis()); tpicker.setcurrenthour(c.get(calendar.hour_of_day)); tpicker.setcurrentminute(c.get(calendar.minute)); di.show(); } }); } public void initbutton6() { mbutton6=(button) findviewbyid(r.id.mbutton4); mbutton6.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent intent = new intent(mainactivity.this, callalarm.class); pendingintent sender = pendingintent.getbroadcast( mainactivity.this,1, intent, 0); alarmmanager am; am = (alarmmanager)getsystemservice(alarm_service); am.cancel(sender); toast.maketext(mainactivity.this,"闹钟时间删除", toast.length_short).show(); settime2.settext("目前无设置"); //sharedpreferences保存数据,并提交 sharedpreferences time3share = getpreferences(2); sharedpreferences.editor editor = time3share.edit(); editor.putstring("time3", "目前无设置"); editor.commit(); } }); } @override public boolean onkeyup(int keycode, keyevent event) { if(keycode == keyevent.keycode_back){ builder = new alertdialog.builder(mainactivity.this) .seticon(r.drawable.clock) .settitle("温馨提示:") .setmessage("您是否要退出大明闹钟程序!!!") .setpositivebutton("确定", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { mainactivity.this.finish(); } }) .setnegativebutton("取消", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { builder.dismiss(); } }).show(); } return true; } private string format(int x) { string s=""+x; if(s.length()==1) s="0"+s; return s; } }
二、callalarm中的代码:
package com.cn.daming; import android.content.context; import android.content.intent; import android.content.broadcastreceiver; import android.os.bundle; public class callalarm extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent i = new intent(context, alarmalert.class); bundle bundleret = new bundle(); bundleret.putstring("str_caller", ""); i.putextras(bundleret); i.addflags(intent.flag_activity_new_task); context.startactivity(i); } }
三、alarmalert中的代码:
package com.cn.daming; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.os.bundle; public class alarmalert extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); new alertdialog.builder(alarmalert.this) .seticon(r.drawable.clock) .settitle("大明闹钟响了!!") .setmessage("快完成你制定的计划吧!!!") .setpositivebutton("关掉它", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { alarmalert.this.finish(); } }) .show(); } }
四、main.xml布局文件的代码:
<?xml version="1.0" encoding="utf-8"?> <absolutelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/other" > <digitalclock android:id="@+id/dclock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="40sp" android:textcolor="@drawable/blue" android:layout_x="70px" android:layout_y="32px" > </digitalclock> <textview android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_title3" android:textsize="20sp" android:textcolor="@drawable/black" android:layout_x="10px" android:layout_y="104px" > </textview> <button android:id="@+id/mbutton1" android:layout_width="wrap_content" android:layout_height="40px" android:text="@string/str_button1" android:textcolor="@drawable/black" android:textsize="18sp" android:layout_x="190px" android:layout_y="102px" > </button> <textview android:id="@+id/settime1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_default" android:textcolor="@drawable/red" android:textsize="16sp" android:layout_x="10px" android:layout_y="149px" > </textview> <button android:id="@+id/mbutton2" android:layout_width="wrap_content" android:layout_height="40px" android:text="@string/str_button2" android:textcolor="@drawable/black" android:textsize="18sp" android:layout_x="190px" android:layout_y="142px" > </button> <textview android:id="@+id/text5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_title4" android:textsize="20sp" android:textcolor="@drawable/black" android:layout_x="10px" android:layout_y="216px" > </textview> <button android:id="@+id/mbutton5" android:layout_width="wrap_content" android:layout_height="40px" android:text="@string/str_button1" android:textcolor="@drawable/black" android:textsize="18sp" android:layout_x="190px" android:layout_y="212px" > </button> <textview android:id="@+id/settime5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_default" android:textcolor="@drawable/red" android:textsize="16sp" android:layout_x="10px" android:layout_y="259px" > </textview> <button android:id="@+id/mbutton6" android:layout_width="wrap_content" android:layout_height="40px" android:text="@string/str_button2" android:textcolor="@drawable/black" android:textsize="18sp" android:layout_x="190px" android:layout_y="252px" > </button> <textview android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_title2" android:textsize="20sp" android:textcolor="@drawable/black" android:layout_x="10px" android:layout_y="326px" > </textview> <button android:id="@+id/mbutton3" android:layout_width="wrap_content" android:layout_height="40px" android:text="@string/str_button1" android:textcolor="@drawable/black" android:textsize="18sp" android:layout_x="190px" android:layout_y="322px" > </button> <textview android:id="@+id/settime2" android:layout_width="170px" android:layout_height="wrap_content" android:text="@string/str_default" android:textcolor="@drawable/red" android:textsize="16sp" android:layout_x="10px" android:layout_y="369px" > </textview> <button android:id="@+id/mbutton4" android:layout_width="wrap_content" android:layout_height="40px" android:text="@string/str_button2" android:textcolor="@drawable/black" android:textsize="18sp" android:layout_x="190px" android:layout_y="362px" > </button> </absolutelayout>
五、timeset.xml布局文件中的代码:
<?xml version="1.0" encoding="utf-8"?> <absolutelayout android:id="@+id/layout2" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <textview android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_text1" android:textcolor="@drawable/white" android:textsize="16sp" android:layout_x="10px" android:layout_y="32px" > </textview> <textview android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_text2" android:textcolor="@drawable/white" android:textsize="16sp" android:layout_x="10px" android:layout_y="172px" > </textview> <timepicker android:id="@+id/tpicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="100px" android:layout_y="12px" > </timepicker> <edittext android:id="@+id/medit" android:layout_width="52px" android:layout_height="40px" android:text="15" android:textsize="16sp" android:layout_x="120px" android:layout_y="162px" > </edittext> <textview android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_text3" android:textcolor="@drawable/white" android:textsize="16sp" android:layout_x="180px" android:layout_y="172px" > </textview> </absolutelayout>
六、string.xml中的代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">hello world, ex06_10</string> <string name="app_name">大明原创闹钟</string> <string name="str_button1">设置闹钟</string> <string name="str_button2">删除闹钟</string> <string name="str_title2">重复响起的闹钟</string> <string name="str_title3">大明闹钟一</string> <string name="str_title4">大明闹钟二</string> <string name="str_default">目前无设置</string> <string name="str_text1">开始时间</string> <string name="str_text2">重复响起的闹钟</string> <string name="str_text3">秒</string> </resources>
七、color.xml中的代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="white">#ffffffff</drawable> <drawable name="other">#ff00fff0</drawable> <drawable name="black">#000000</drawable> <drawable name="blue">#0000ff</drawable> <drawable name="red">#ff0000</drawable> </resources>
八、androidmanifest.xml中的代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn.daming" android:versioncode="1" android:versionname="1.0"> <uses-sdk android:minsdkversion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <!-- 註冊receiver callalarm --> <receiver android:name=".callalarm" android:process=":remote" /> <activity android:name=".mainactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".alarmalert" android:label="@string/app_name"> </activity> </application> </manifest>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: oracle数据库常用的99条查询语句