3

我想在特定日期设置警报并发出通知。然后我目前正在使用带有 NotificationManager 的 AmarmManager。当我从 dateDialog 设置选定日期时,警报正在工作。如何将日历值放在固定时间的闹钟上?我想每天在固定时间重复闹钟,比如早上 9:00。目前,警报忽略特定日期的时间。你可以帮帮我吗?非常感谢。

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);
         }  
}
4

4 回答 4

10

如果您想在月份的特定日期播放警报,假设您想在 2012 年 6 月 11 日播放警报,那么您应该这样指定,但 6 月是 6 个月,那么您应该在 Calender.Month 中指定 5。

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);  
于 2012-06-11T10:44:18.140 回答
3

嘿,这是如何将 android AlarmManager 上的警报设置为特定日期(android alarmmanager 设置警报日期)我一直在寻找这个。注意月值!!

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);
于 2012-12-18T16:49:38.713 回答
2

你应该打电话public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)见这里)重复警报。例如,您想在每天上午 9:00 发出警报,您可以这样做:

Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 9);
c.set(Calendar.MINUTE, 0);
setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);

另外,在初始化 PendingIntent 时将最后一个参数设置为 0。

PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this, 
                        0, myIntent, 0);
于 2011-12-01T12:20:36.090 回答
0
cal_alarm.setTime(dat);
        cal_alarm.set(Calendar.MONTH,3);
        cal_alarm.set(Calendar.YEAR,2012);
        cal_alarm.set(Calendar.DAY_OF_MONTH,4);
        cal_alarm.set(Calendar.HOUR_OF_DAY,19);//set the alarm time
        cal_alarm.set(Calendar.MINUTE, 15);
        cal_alarm.set(Calendar.SECOND,0);


        if(cal_alarm.before(cal_now)){//if its in the past increment
            cal_alarm.add(Calendar.DATE,1);
        }

        Intent intent = new Intent(AlarmManagerTestActivity.this,
                AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            AlarmManagerTestActivity.this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);  
}

这可能会帮助你

于 2012-05-05T06:03:20.443 回答