小编ind*_*der的帖子

警报管理器 - 计划多个非重复事件

在Android Alarm Manager中,我们如何安排多个不重复且没有固定间隔重复的警报?我无法使用'setRepeating'功能,因为警报没有任何重复模式.

我将警报时间存储在Sqlite数据库表中,活动应该从该表中选择日期和时间并设置警报.

如果我们在循环中设置不同的警报,那么它只保留最后一个.我从帖子中读到:如何创建多个警报?

它告诉将唯一的Id附加到意图,然后设置单个警报事件.但它对我不起作用.

我们需要在Manifest文件中添加一些东西来处理这个唯一的id吗?

活动'RegularSchedule'中的代码是,它只创建一个警报事件:

        while (notifCursor.moveToNext()) {
            Intent intent = new Intent(RegularSchedule.this,
                    RepeatingAlarm.class);

            // The cursor returns first column as unique ID             
            intent.setData(Uri.parse("timer:" + notifCursor.getInt(0)));

            PendingIntent sender = PendingIntent.getBroadcast(
                    RegularSchedule.this, 0, intent, 0);

            // Setting time in milliseconds taken from database table 
            cal.setTimeInMillis(notifCursor.getLong(1));

            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
        }
Run Code Online (Sandbox Code Playgroud)

如果需要更多详细信息或代码段,请与我们联系.

清单文件(此处为RepeatingAlarm扩展BroadcastReceiver):

    <receiver android:name=".user_alerts.RepeatingAlarm" android:process=":remote" />

    <activity android:name=".user_alerts.RegularSchedule"
        android:label="@string/reg_schedule_title" android:theme="@android:style/Theme.Light">
    </activity>
Run Code Online (Sandbox Code Playgroud)

RepeatingAlarm:

public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, …
Run Code Online (Sandbox Code Playgroud)

android android-alarms

24
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

android-alarms ×1