Android 设置多个闹钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12785702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:05:00  来源:igfitidea点击:

Android Set Multiple Alarms

androidandroid-alarms

提问by Sammy

I'm trying to implement an Android app that needs to alarm (or to alert) multiple times along the time.

我正在尝试实现一个需要多次报警(或提醒)的 Android 应用程序。

I've already searched, but the nearest I found was a fixed-number of alarms set, and I guess the example didn't work.

我已经搜索过了,但我找到的最近的是固定数量的警报设置,我猜这个例子不起作用。

What I want to know if there is exists an approach to dynamically set multiple alarms, like an Array of alarms and then to trigger those alarms in their specific timestamps.

我想知道是否存在一种动态设置多个警报的方法,例如警报数组,然后在其特定时间戳中触发这些警报。

回答by Nikolai Samteladze

If you want to set multiple alarms (repeating or single), then you just need to create their PendingIntents with different requestCode. If requestCodeis the same, then the new alarm will overwrite the old one.

如果你想设置多个闹钟(重复或单个),那么你只需要用PendingIntent不同的requestCode. 如果requestCode相同,则新闹钟将覆盖旧闹钟。

Here is the code to create multiple single alarms and keep them in ArrayList. I keep PendingIntent's in the array because that's what you need to cancel your alarm.

这是创建多个单个警报并将它们保存在ArrayList. 我将PendingIntent's保留在数组中,因为这是取消闹钟所需要的。

// context variable contains your `Context`
AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

for(i = 0; i < 10; ++i)
{
   Intent intent = new Intent(context, OnAlarmReceiver.class);
   // Loop counter `i` is used as a `requestCode`
   PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);
   // Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
   mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                SystemClock.elapsedRealtime() + 60000 * i, 
                pendingIntent); 

   intentArray.add(pendingIntent);
}

Also, see this question: How to set more than one alarms at a time in android?.

另外,请参阅这个问题:如何在 android 中一次设置多个闹钟?.

回答by iamkristher

You can set the repetition of the alarm:

您可以设置闹钟的重复次数:

in this case:

在这种情况下:

public void AddAlarm(int requestCode,MutableDateTime dueDate,int repeat) {
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra(Constants.RECORD_ID, requestCode);
        intent.putExtra("REPEAT", repeat);
        PendingIntent operation = PendingIntent.getBroadcast(context, requestCode, intent,  PendingIntent.FLAG_ONE_SHOT );
        MutableDateTime due = dueDate.toMutableDateTime();
        switch(repeat){
        case NO_REPEAT:
            due.addMinutes(0);
            break;
        case DAILY:

            due.addDays(1); 
            break;
        case WEEKLY:
            due.addWeeks(1);
            break;
        case MONTHLY:
            due.addMonths(1);
            break;
        case MONTHLY_2:
            due.addWeeks(5);            
            break;
        case YEARLY:
            due.addYears(1);
            break;
        }
        due.add(-(dueDate.getMillis()));
        due.setSecondOfMinute(0);
        dueDate.setSecondOfMinute(0);
        alarm.cancel(operation);
        alarm.set(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), operation);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), due.getMillis(), operation);
}

回答by Junaid

To dynamically set up multiple alarms, the approach which I used is that I created a single alarm. Then in my alarm setting class, a static integer (to be used as requestcode) is initialized which will be incremented each time from my main activity whenever I click on "add alarm" button in my main activity. E.g.

为了动态设置多个警报,我使用的方法是创建单个警报。然后在我的警报设置类中,一个静态整数(用作请求代码)被初始化,每当我点击我的主要活动中的“添加警报”按钮时,它都会从我的主要活动中增加。例如

MainActivity.java

主活动.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void addAlarmClick(View v) {
    AlarmActivity.broadcastCode++;
    startActivity(new Intent(this, AlarmActivity.class));
}
}

AlarmActivity.java

报警活动.java

public class AlarmActivity extends AppCompatActivity {`

public static int broadcastCode=0;
/*some code here*/
Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this,
                            broadcastCode, myIntent, 0);

I hope this will help.

我希望这将有所帮助。