Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - how to set an alarm to a specific date

Tags:

I have seen a lot of tutorials and been trying for 2 hours now , though something is still wrong. I am very nervous now :) I want to set an alarm e.g. to 16:25 to go off, but nothing happens. I have this code:

   Calendar cur_cal = new GregorianCalendar();
    cur_cal.setTimeInMillis(System.currentTimeMillis());

    Calendar cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY, 16);
    cal.set(Calendar.MINUTE, 25);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

I have also tried this:

 cal.set(Calendar.AM_PM, cur_cal.get(Calendar.AM_PM));
    cal.set(Calendar.HOUR, 4);

My final goal is to make it a repeating alarm, e.g. it should go off every day at the set time.

Update 01.17.2011. Still not working. I have this code:

           Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, 2011);
            cal.set(Calendar.MONTH, Calendar.JANUARY); 
            cal.set(Calendar.DAY_OF_MONTH, 17);
            cal.set(Calendar.HOUR_OF_DAY, 16);
            cal.set(Calendar.MINUTE, 58);
         cal.set(Calendar.SECOND, 0);
          cal.set(Calendar.MILLISECOND, 0);

I have also tried this:

 cal.set(Calendar.HOUR, 4);
 cal.set(Calendar.AM_PM, Calendar.PM);

and this:

cal.set(Calendar.HOUR_OF_DAY, 4 );
            cal.set(Calendar.AM_PM, Calendar.PM);
like image 211
erdomester Avatar asked Jan 15 '11 15:01

erdomester


People also ask

How do you set an alarm for a certain date?

Open the Clock app, and tap the down-arrow below the ON/OFF toggle on an alarm. In the expanded view, check "Repeat", and tap the circles for days you want the alarm to go off on.

How do you set an alarm for a future date on a Samsung?

This is achieved by tapping on the calendar icon that sits just above the row of letters that represents each day. A small calendar window will open up, allowing you tap the day you'll need the alarm. Tap Done once you've selected the date. Underneath these you will find a field called Alarm name.

Can I set an alarm for a specific date on iPhone?

Unfortunately, you cannot set an alarm for a later date. You can however use Reminders or Calendar to notify you. Cheers!


2 Answers

package your.pack.name;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class AlarmActivity extends Activity {
    /** Called when the activity is first created. */

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

        Calendar cal = Calendar.getInstance();

        cal.setTimeInMillis(System.currentTimeMillis());
        cal.clear();
        cal.set(2012,2,8,18,16);

        AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
       // cal.add(Calendar.SECOND, 5);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);


    }
}
like image 131
Chandrakant Kadam Avatar answered Sep 28 '22 21:09

Chandrakant Kadam


Usually you shouldn't obtain Calendar like you do, there is Calendar.getInstance() method for that:

Calendar cal = Calendar.getInstance();

That gives you a calendar with all fields set to current date, then just:

cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 25);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
like image 32
Konstantin Burov Avatar answered Sep 28 '22 22:09

Konstantin Burov