/** * trigger the Broadcast event and set the alarm */ public class ReminderSetting extends Activity {
Button btnEnable;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
/* create a button. When you click the button, the alarm clock is enabled */ btnEnable=(Button)findViewById(R.id.btnEnable); btnEnable.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setReminder(true); } }); }
/** * Set the alarm * * @param b whether enable the Alarm clock or not */ private void setReminder(boolean b) {
// get the AlarmManager instance AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE); // create a PendingIntent that will perform a broadcast PendingIntent pi= PendingIntent.getBroadcast(ReminderSetting.this, 0, new Intent(this,MyReceiver.class), 0);
if(b){ // just use current time as the Alarm time. Calendar c=Calendar.getInstance(); // schedule an alarm am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); } else{ // cancel current alarm am.cancel(pi); }
/** * Receive the broadcast and start the activity that will show the alarm */ public class MyReceiver extends BroadcastReceiver {
/** * called when the BroadcastReceiver is receiving an Intent broadcast. */ @Override public void onReceive(Context context, Intent intent) {
/* start another activity - MyAlarm to display the alarm */ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(context, MyAlarm.class); context.startActivity(intent);