Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

calendar - Android alarm setting with specific date

I wan to set alarm with notification at specific date. Then I am using AmarmManager with NotificationManager currently. When I set selected date from dateDialog, the alarm is working. How can I put calendar value on alarm set with fixed time? I want to repeat alarm every day on fixed time such as at 9:00 in the morning. Currently, alarm is ignoring the time on specific date. Could you help me? Many thanks.

confirmButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) {
            //set alarm with expiration date                
            am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            setOneTimeAlarm();
            Toast.makeText(fridgeDetails.this, "Alarm automatic set", Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK);
            finish();
        }
        public void setOneTimeAlarm() {
        c.set(Calendar.HOUR_OF_DAY, 10);
        c.set(Calendar.MINUTE, 0);
        c.set(expiredYear, expiredMonth, expiredDay);
        Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this, 
                        0, myIntent, PendingIntent.FLAG_ONE_SHOT);
        am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);

        }
    });


private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() 
{
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        expiredYear = year;
        expiredMonth = monthOfYear;
        expiredDay = dayOfMonth;
        displayDate();
    }
};  

public class AlarmService extends BroadcastReceiver{
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
          CharSequence from = "Check your fridge";
          CharSequence message = "It's time to eat!";
          PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(), 0);
          Notification notif = new Notification(R.drawable.ic_launcher,
            "Keep Fridge", System.currentTimeMillis());
          notif.setLatestEventInfo(context, from, message, contentIntent);
          nm.notify(1, notif);
         }  
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to play alarm for particular date of months suppose you want to play alarm on 11-june-2012 then you should specify like this but June is 6 month then you should specify 5 in Calender.Month.

Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,5);
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.DAY_OF_MONTH,11);

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

Intent _myIntent = new Intent(getApplicationContext(), ReceiverClass.class);
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);
AlarmManager _myAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//_myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), _myPendingIntent);
_myAlarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent);  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...