prakash_d22
prakash_d22

Reputation: 1163

set alarm on selected time and date

I am developing a taskmanager on Android 2.1. I want to set alarm for a task set by date from datepicker and time from time picker Help me with the code..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText next = (EditText) findViewById(R.id.editText1);
    final Button sub = (Button) findViewById(R.id.button1);
    final Button res = (Button) findViewById(R.id.button2);
    final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
    final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);

    sub.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View view) {

             int y=dp.getYear();
             int mo=dp.getMonth();
             int day=dp.getDayOfMonth();

            Time t = new Time();
                                    int h=t.getCurrentHour();
                                    int m=t.getCurrentMinutes();

    }

 private AlarmManager getSystemService(String alarmService) {

            // TODO Auto-generated method stub
            return null;
        }

    });

Upvotes: 4

Views: 10262

Answers (4)

Zeev G
Zeev G

Reputation: 2221

Hey this is how to set alarm on android AlarmManager to spesific date (android alarmmanager set alarm on date) I have been searching all over for this. pay attention to the value of the month!!

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
//cal.add(Calendar.SECOND, 10);

cal.set(Calendar.DATE,19);  //1-31
cal.set(Calendar.MONTH,Calendar.DECEMBER);  //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR,2012);//year...

cal.set(Calendar.HOUR_OF_DAY, 16);  //HOUR
cal.set(Calendar.MINUTE, 39);       //MIN
cal.set(Calendar.SECOND, 10);       //SEC


// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(MainActivity.this, alarmAct.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0);

//or if you start an Activity
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

Upvotes: 8

user829589
user829589

Reputation: 179

public class AlarmService extends Activity {
    private PendingIntent mAlarmSender;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create an IntentSender that will launch our service, to be scheduled
        // with the alarm manager.
        mAlarmSender = PendingIntent.getService(AlarmService.this,
                0, new Intent(AlarmService.this, AlarmService_Service.class), 0);

        setContentView(R.layout.alarm_service);

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.start_alarm);
        button.setOnClickListener(mStartAlarmListener);
        button = (Button)findViewById(R.id.stop_alarm);
        button.setOnClickListener(mStopAlarmListener);
    }

    private OnClickListener mStartAlarmListener = new OnClickListener() {
        public void onClick(View v) {
            // We want the alarm to go off 30 seconds from now.
            long firstTime = SystemClock.elapsedRealtime();

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            firstTime, 30*1000, mAlarmSender);

            // Tell the user about what we did.
            Toast.makeText(AlarmService.this, R.string.repeating_scheduled,
                    Toast.LENGTH_LONG).show();
        }
    };

    private OnClickListener mStopAlarmListener = new OnClickListener() {
        public void onClick(View v) {
            // And cancel the alarm.
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(mAlarmSender);

            // Tell the user about what we did.
            Toast.makeText(AlarmService.this, R.string.repeating_unscheduled,
                    Toast.LENGTH_LONG).show();

        }
    };
}

Upvotes: 0

bwoogie
bwoogie

Reputation: 4427

This is how to set an alarm.

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());

//when does this go off?
Long mHowLongFromNowInMilliseconds = 10000 //(10 seconds from now)
calendar.add(Calendar.MILLISECOND, mHowLongFromNowInMilliseconds);

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

Calendar isnt required... But it can be helpful ;)

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent); 

!!! Remember: Alarms are canceled when the device is powered off completely.

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

Upvotes: 2

Abhinava
Abhinava

Reputation: 1030

Android do not play alarm for you, Alarm Manager allow you to schedule your application to be run at some point in the future. So, simply add the time and a pending intent in the AlarmManager and when this intent will be invoked, play music.

Visit: http://developer.android.com/reference/android/app/AlarmManager.html

Upvotes: 1

Related Questions