当我更改日期时,Android 闹钟会在任何日期运行通知

发布于 2024-12-19 23:47:11 字数 1956 浏览 3 评论 0原文

我将 AlarmManagerNotificationManagerBroadcastReceiver 一起使用。 当我设置特定日期的闹钟时,它第一次起作用。但是,当我修改日期并单击确认按钮时,闹钟在任何日期都有效 立即地。我想在到期日期后的固定时间设置警报间隔。 它有什么问题呢?我暂时不明白。

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, 14);
    c.set(Calendar.MINUTE, 49);
    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.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, pendingIntent);
}
}); 

AlarmService.java

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);
        notif.defaults |= Notification.DEFAULT_SOUND; 
        notif.flags |= Notification.FLAG_AUTO_CANCEL; 
        nm.notify(1, notif);
    }   
}

I am using AlarmManager and NotificationManager with BroadcastReceiver.
When I set an alarm with a specific date, it is working the first time. However, when I modify the date and click the confirm button, the alarm is working any date
immediately. I want to set the alarm interval day after expired date with fixed time.
What is the problem with it? I don't understand at the moment.

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, 14);
    c.set(Calendar.MINUTE, 49);
    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.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, pendingIntent);
}
}); 

AlarmService.java

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);
        notif.defaults |= Notification.DEFAULT_SOUND; 
        notif.flags |= Notification.FLAG_AUTO_CANCEL; 
        nm.notify(1, notif);
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-12-26 23:47:11

您需要设置该属性而不是 FLAG_ONE_SHOT。这是针对单个警报事件,而不是针对重复事件。尝试一下,

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

此处查看更多详细信息

编辑:

正如您对 PendingIntent 的通知所做的那样

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);

,在这里您传递 Intent 的空对象,您需要传递类名称,当您单击通知时,将启动哪个 Activity,就像在闹钟设置时间中一样,您

Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
        fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

现在只需传递 Acitivity 名称假设你想要的意图启动您的家庭活动和活动名称,例如“homeactivity”

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,HomeActivity.class), 0);

You need to set the property instead of FLAG_ONE_SHOT. This is for single alarm event not for the repeat. try this

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

see more detail about from here

Edit:

As you do for notification with PendingIntent

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);

In this you pass the empty object of Intent you need to pass the class name for that when you click on notification which Activity will be launch like in Alarm set time you do

Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
        fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

now just pass the Acitivity name in the intent like suppose you want to launch your home activity and activity name like "homeactivity"

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,HomeActivity.class), 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文