具有特定日期的 Android 闹钟设置 [英] Android alarm setting with specific date

查看:29
本文介绍了具有特定日期的 Android 闹钟设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

你应该调用public void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)(看这里) 重复警报.例如,你想在每天早上 9:00 发出警报,你可以这样做:

You should call public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)(see here) to repeat the alarm. For example, you want to fire the alarm on 9:00 am every day, you can do :

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.

Also,set the last parameter to 0 when initilazing the PendingIntent.

PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this, 
                        0, myIntent, 0);

这篇关于具有特定日期的 Android 闹钟设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆