如何创建多个警报?

发布于 2024-11-19 04:48:24 字数 1043 浏览 2 评论 0原文

我可以使用下面的代码创建和取消警报。 我想创建多个闹钟。闹钟时间来自数组列表。在此数组列表中,我想为每个日期创建一个警报。按下取消按钮将仅取消当前警报。我该怎么做呢?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    setOneTimeAlarm(); 

    buttonCancel.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);

            // Tell the user about what we did.
            Toast.makeText(MainActivity.this, "Cancel!", Toast.LENGTH_LONG).show();
        }
    });
}

private void setOneTimeAlarm() {
    Intent intent = new Intent(this, AReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

I am able to create and cancel an alarm with the code below.
I want to create more than one alarm. Alarm times comes from an arraylist. In this arraylist I would like to create an alarm for each date. And presses of the cancel button will cancel only the current alarm. How can I do it?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    setOneTimeAlarm(); 

    buttonCancel.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);

            // Tell the user about what we did.
            Toast.makeText(MainActivity.this, "Cancel!", Toast.LENGTH_LONG).show();
        }
    });
}

private void setOneTimeAlarm() {
    Intent intent = new Intent(this, AReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

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

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

发布评论

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

评论(2

最佳男配角 2024-11-26 04:48:24

我将发布一个示例,其中包含我在我创建的应用程序之一中执行此操作的方法。

调用警报时,您需要发送一个额外的 ID,如下所示:
Tmp 是一个具有唯一 ID 的对象。

Intent intent = new Intent(DisplayActivity.this,AlarmReciever.class);

intent.setData(Uri.parse("timer:"+tmp.getId()));

PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayActivity.this,1, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);

然后使用此行检索 id:

Long.parseLong(intent.getData().getSchemeSpecificPart())

取消时只需使用 ID 创建相同的挂起意图并调用 Alarmmanager.cancel()

编辑:

要取消用户单击的项目上的特定警报(这里是 tmp.getId 的位置)方便)我刚刚使用了这段代码,我认为您需要创建相同的pendingIntent才能取消特定的警报。

 Intent intent = new Intent(DisplayActivity.this,AlarmReciever.class);
 intent.setData(Uri.parse("timer:"+tmp.getId()));
 PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayActivity.this,1, intent, 0);
 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
 am.cancel(pendingIntent);

AlarmReciever 是我的广播接收器。 DisplayActivity 是活动本身。

在 AlarmReciever 中我有以下代码:

Long.parseLong(intent.getData().getSchemeSpecificPart())

I'll post an example with a way I did it in one of the apps I created.

When calling the alarm you need to send an extra id like this:
Tmp is an object which has a unique ID.

Intent intent = new Intent(DisplayActivity.this,AlarmReciever.class);

intent.setData(Uri.parse("timer:"+tmp.getId()));

PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayActivity.this,1, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);

and then you retrieve the id with this line:

Long.parseLong(intent.getData().getSchemeSpecificPart())

When cancelling just create the same pending intent with the ID and call alarmmanager.cancel()

Edit:

To cancel the specific alarm on the item the user clicked on (here is where tmp.getId comes in handy) I just used this code, I think you need to create the same pendingIntent to be able to cancel the specific alarm.

 Intent intent = new Intent(DisplayActivity.this,AlarmReciever.class);
 intent.setData(Uri.parse("timer:"+tmp.getId()));
 PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayActivity.this,1, intent, 0);
 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
 am.cancel(pendingIntent);

AlarmReciever is my BroadcastReceiver. DisplayActivity is the activity itself.

It's in AlarmReciever I have the following code:

Long.parseLong(intent.getData().getSchemeSpecificPart())
ˉ厌 2024-11-26 04:48:24

这是 David 关于如何获得唯一 ID 的回复的延续。

唯一ID可以来自SQLITE表。创建表来存储提醒日期和时间时,请使用 _ID 列和 AUTOINCRMENT。当插入行时,该列将自动获取单数和唯一数。该表还将帮助您存储提醒,以防设备关闭并且您想再次重新创建提醒。在设备关闭时,我认为警报会丢失。

CREATE TABLE IF NOT EXISTS reminder_datetime
_ID INTEGER PRIMARY KEY AUTOINCREMENT,
NOTIF_DATETIME INTEGER;

要从表中设置和获取日期,请使用 Calendar 对象:

Calendar cal = Calendar.getInstance();

然后您可以使用 cal.setTime 或 cal.setTimeInMillis 函数。
同样,获取函数来获取日期。

如果要将日期格式设置为可读格式,请使用日期格式:

myDate = android.text.format.DateFormat.format("MM/dd/yyyy", cal.getTimeInMillis()).toString();

This is on continuation of David's reply on how to get unique ID.

The unique ID can come from the SQLITE table. While creating the table to store the reminder date and time, have an _ID column with AUTOINCREMENT. When rows are inserted, this column will automatically get one-up numbers and unique ones. The table will also help you store the reminders for you in case the device is switched off and you want to recreate the reminders again. On device shutdown, I think alarms will get lost.

CREATE TABLE IF NOT EXISTS reminder_datetime
_ID INTEGER PRIMARY KEY AUTOINCREMENT,
NOTIF_DATETIME INTEGER;

To work with setting and getting dates from the table, use Calendar object:

Calendar cal = Calendar.getInstance();

Then you can use, cal.setTime or cal.setTimeInMillis functions.
Similarly, get functions to get the date.

Use date format if want to format the date to readable one:

myDate = android.text.format.DateFormat.format("MM/dd/yyyy", cal.getTimeInMillis()).toString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文