This application implements a simple alarm function, and can also be alerted to the alarm clock when the program is closed.
A problem is left here :
If I close the program by applying itself, I can do what I mentioned above, but if I implement the application shutdown in the phone process management, the reminder doesn't work, and I'm puzzled I've closed the application itself and I can't see it in the process. So who knows to help leave a message, thank you.
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 the activity is a.
* * @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) {MC
Alendar.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 VI
ew, 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 (+, "ddd", "FFFF", Mcalendar.gettimeinmillis ());
}, Mhour, Mminute, True). Show ();
}
}); @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = keyevent.keycode_back) {Showback
Dialog ();
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 ("hint"). SetIcon (R.drawable.icon). Setmessage ("Exit?") . 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
the 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 ;
}
Read so much, estimated that some people have wearies, I only put the code is to hope that everyone can see the code, and not the computer up and down one after another source files, only know to use and do not care about the technical points, so you can not lift a third.
SOURCE Download: Http://xiazai.jb51.net/201607/yuanma/ClockDemo (jb51.net). rar
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.