Android - how to set an alarm to a specific date

后端 未结 3 755
故里飘歌
故里飘歌 2020-11-30 01:08

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, bu

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 02:06

    The following code works perfectly for alarm. The date and time i mentioned here is : 2012- June- 28, 11:20:00 AM. And the most important thing is, month is specified from 0 t0 11 only. Means June should be specified by 5.

            AlarmManager objAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);              
            Calendar objCalendar = Calendar.getInstance();
            objCalendar.set(Calendar.YEAR, 2012);
            //objCalendar.set(Calendar.YEAR, objCalendar.get(Calendar.YEAR));
            objCalendar.set(Calendar.MONTH, 5);
            objCalendar.set(Calendar.DAY_OF_MONTH, 28);
            objCalendar.set(Calendar.HOUR_OF_DAY, 11);
            objCalendar.set(Calendar.MINUTE, 20);
            objCalendar.set(Calendar.SECOND, 0);
            objCalendar.set(Calendar.MILLISECOND, 0);
            objCalendar.set(Calendar.AM_PM, Calendar.AM);         
    
            Intent alamShowIntent = new Intent(this,AlarmActivity.class);
            PendingIntent alarmPendingIntent = PendingIntent.getActivity(this, 0,alamShowIntent,0 );
    
            objAlarmManager.set(AlarmManager.RTC_WAKEUP,objCalendar.getTimeInMillis(), alarmPendingIntent);
    

提交回复
热议问题