简单实现Android闹钟程序 附源码
程序员文章站
2024-03-03 21:31:40
这个应用实现了简单的闹钟功能,关闭程序的状态中也可以进行闹钟的提醒。
这里遗留了一个问题:
如果我通过应用本身关闭程序,是可以实现我上述所说的功能,但是如果我在手机进程...
这个应用实现了简单的闹钟功能,关闭程序的状态中也可以进行闹钟的提醒。
这里遗留了一个问题:
如果我通过应用本身关闭程序,是可以实现我上述所说的功能,但是如果我在手机进程管理中实现应用程序的关闭操作,这个提醒就不起作用了,我很疑惑的是,我通过应用程序本身关闭了后,在进程中也是查看不到这个应用了的。所以哪位知道的帮留个言指点下,谢谢。
clockdemo.java
package com.example.clock; import java.util.calendar; import android.app.activity; import android.app.alertdialog; import android.app.timepickerdialog; import android.content.dialoginterface; import android.os.bundle; import android.view.keyevent; import android.view.view; import android.widget.button; import android.widget.timepicker; public class clockdemo extends activity { private button mset; calendar mcalendar = calendar.getinstance(); /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); objectpool.malarmhelper = new alarmhelper(this); mset = (button) findviewbyid(r.id.mset); setlistener(); } public void setlistener() { mset.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { mcalendar.settimeinmillis(system.currenttimemillis()); int mhour = mcalendar.get(calendar.hour_of_day); int mminute = mcalendar.get(calendar.minute); new timepickerdialog(clockdemo.this, new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view, int hourofday, int minute) { mcalendar.settimeinmillis(system .currenttimemillis()); mcalendar.set(calendar.hour_of_day, hourofday); mcalendar.set(calendar.minute, minute); mcalendar.set(calendar.second, 0); mcalendar.set(calendar.millisecond, 0); objectpool.malarmhelper.openalarm(32, "ddd", "ffff", mcalendar.gettimeinmillis()); } }, mhour, mminute, true).show(); } }); } @override public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { showbackdialog(); return true; } return super.onkeydown(keycode, event); } /** give the tip when exit the application. */ public void showbackdialog() { final alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("提示") .seticon(r.drawable.icon) .setmessage("是否退出?") .setpositivebutton("sure", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { system.exit(0); android.os.process .killprocess(android.os.process.mypid()); dialog.dismiss(); } }) .setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); alertdialog ad = builder.create(); ad.show(); } }
alarmhelper.java
package com.example.clock; import android.app.alarmmanager; import android.app.pendingintent; import android.content.context; import android.content.intent; public class alarmhelper { private context c; private alarmmanager malarmmanager; public alarmhelper(context c) { this.c = c; malarmmanager = (alarmmanager) c .getsystemservice(context.alarm_service); } public void openalarm(int id, string title, string content, long time) { intent intent = new intent(); intent.putextra("_id", id); intent.putextra("title", title); intent.putextra("content", content); intent.setclass(c, callalarm.class); pendingintent pi = pendingintent.getbroadcast(c, id, intent, pendingintent.flag_update_current); malarmmanager.set(alarmmanager.rtc_wakeup, time, pi); } public void closealarm(int id, string title, string content) { intent intent = new intent(); intent.putextra("_id", id); intent.putextra("title", title); intent.putextra("content", content); intent.setclass(c, callalarm.class); pendingintent pi = pendingintent.getbroadcast(c, id, intent, 0); malarmmanager.cancel(pi); } }
callalarm.java
package com.example.clock; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; public class callalarm extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent.setclass(context, alarmalert.class); intent.addflags(intent.flag_activity_new_task); context.startactivity(intent); } }
alarmalert.java
package com.example.clock; 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("ddd") .setmessage("fff") .setpositivebutton("ddd", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { system.exit(0); android.os.process .killprocess(android.os.process.mypid()); } }).show(); } } objectpool.java public class objectpool { /** the alarm object. */ public static alarmhelper malarmhelper; }
看了那么多,估计某些人又嫌烦了,我只贴上代码的目的是希望大家能看代码,而不是电脑上下了一个又一个的源文件,只知道用而不关心其中的技术点,这样就不能举一番三了。
源码下载:http://xiazai.jb51.net/201607/yuanma/clockdemo(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java中泛型的用法总结
下一篇: java 可变参数详解及实例