ScorpioBlue
ScorpioBlue

Reputation: 187

Creating an alarm at a certain date and time

I am trying to get an alarm or notification to go at a certain time and date. I want to be able to have multiple alarms for specific dates in an array. I don't know where to continue off of having my button working:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Button button = (Button)findViewById(R.id.button1);
   // button.setOnClickListener(this);
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
    final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) {

             if (checkBox.isChecked()) {



             }

        }

Thank you in advance for helping me.

Upvotes: 3

Views: 2245

Answers (2)

Nate
Nate

Reputation: 401

You're going to want to use the Alarm Manager

This tutorial is a pretty simple example how to use it.

Another simple exmaple that breaks it down into the basic steps of

  1. Creating an event
  2. Creating a BroadcastReceiver to handle the event

EDIT
To make a notification, modify the broadcast receiver to create a notification when the alarm event is received.

Android Status Notification Documentation shows pretty well the basic steps to create a notification.

  1. Get a reference to the NotificationManager
  2. Create a notification instance. You can then define an image, text, and time which would just be the current time because the alarm event already defined when to go off
  3. Define the notification content such as message and intent to launch when cliked
  4. Call the notification

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) receiverContext.getSystemService(ns);

Notification notification = new Notification(R.drawable.notification_icon, "Hello", System.currentTimeMillis());

CharSequence contentTitle = "My notification";  
CharSequence contentText = "Hello World!";  
Intent notificationIntent = new Intent(receiverContext, TargetActivity.class);  
PendingIntent contentIntent = PendingIntent.getActivity(receiverContext, 0, notificationIntent, 0);  

notification.setLatestEventInfo(receiverContext, contentTitle, contentText, contentIntent);  

private static final int notificationID = 1;

mNotificationManager.notify(notificationID, notification);

EDIT 2
Creating notification at specified time using Alarm Manager

Upvotes: 3

Shubham
Shubham

Reputation: 820

You can try like this:

ArrayList<Calendar> alarmTimes = getAlarmTimes(); //Make your own function which returns alarm times as an array of Calendar type

for(int i=0; i<alarmTimes.size(); i++)
{
    Intent intent = new Intent(this, YourClass.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTimes[i].getTimeInMillis(), pendingIntent);
}

If you would like to handle each alarm independently, then use a different requestCode for each alarm. Also, you might want to take a look at the different flags being used in alarmManager.set() function. Modify them according to your application needs.

http://developer.android.com/reference/android/app/PendingIntent.html

You will have to make an AlarmReceiver class which will handle whatever you want to do when an alarm goes off (make sure to add this class in AndroidManifest as receiver). If you want to send extra info along with an alarm, then you use intent extras like this:

intent.putExtra("info1", value1);
intent.putExtra("info2", value2);

where info1 is the name of the extra data, and value1 is the value which you send.

Hope this helps!

EDITED:

ArrayList<Calendar> alarmTimes = new ArrayList<Calendar>();

Calendar calendar = Calendar.getInstance();
calendar.set(CALENDAR.MONTH, 7);
calendar.set(CALENDAR.DAY_OF_MONTH, 17);
calendar.set(CALENDAR.HOUR_OF_DAY, 13);
calendar.set(CALENDAR.MINUTE, 0);

alarmTimes.add(calendar);

//Repeat the above code for all alarm times and then return array object
return alarmTimes;

Upvotes: 1

Related Questions