Android - 避免用户更改日期/时间时触发警报

标签 android broadcastreceiver alarmmanager android-alarms android-broadcastreceiver

我有一个应用程序通过 AlarmManager 中的 setRepeating 方法在一周中的每一天发送定期警报:

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(INTENT_TAG_ALERT_ID, lastAlertId.getId()+"");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, idRandom, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cDate.getTimeInMillis(), ((AlarmManager.INTERVAL_DAY)*7), pendingIntent);

然后在 list 上定义为接收器的 AlarmReceiver 上获取警报:

    <receiver
        android:name=".manager.alarm.AlarmReceiver"
        android:exported="true">
    </receiver> 

问题是当用户在未来一天或两天内通过他的手机更改日期/时间时,我会收到来自接收者的多个电话(取决于选择的天数)。因为我使用了 setRepeating 方法,所以我无法验证警报是否在正确的时间发送。

我知道这是正常程序,如下所述: "A trigger time. If the trigger time you specify is in the past, the alarm triggers immediately." , 但我想阻止它。

我想放置一个监听器来了解用户何时更改手机的日期和时间,取消安排所有警报,然后重新安排它们,如下所示:

public class DateTimeChangedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals(Intent.ACTION_TIMEZONE_CHANGED) || action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_DATE_CHANGED)) {
        AlarmScheduler.unscheduleAllAlarms();
        //and then
        AlarmScheduler.scheduleAllAlarms();
    }
}
}

和 list :

        <receiver android:name=".manager.alarm.DateTimeChangedBroadcastReceiver"
        android:priority="1000">
        <intent-filter>
            <action android:name="android.intent.ACTION_TIMEZONE_CHANGED"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.ACTION_TIME"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.TIME_SET"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.ACTION_TIME_CHANGED"/>
        </intent-filter>

    </receiver>

但是在这个解决方案中,时间变化的接收者是在警报的接收者之后,所以它对我没有多大帮助...

有没有办法选择接收者的顺序?

有更好的方法吗?

我也考虑过让一个监听器来存储时钟时间。如果用户更改时间,我应该只验证差异...但我相信我可以找到更好的方法来做到这一点(最好不涉及应用程序上的另一个听众)

最佳答案

你不需要多个接收者来处理这个。在以下位置执行以下操作:

AlarmReceiver.java

    @Override
public void onReceive(Context context, Intent intent) {


    //Handle the logics here.
    //This function is called every time alarm repeats.
    //So simply find out if you need an alarm right now and pop alarm.
}

XML

  <receiver android:name=".manager.alarm.AlarmReceiver"
         android:exported="true">
        <intent-filter>
            <action android:name="android.intent.ACTION_TIMEZONE_CHANGED"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.ACTION_TIME"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.TIME_SET"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.ACTION_TIME_CHANGED"/>
        </intent-filter>

    </receiver>

根据您的时间间隔或日期更改等调用警报接收器...然后您可以检测导致警报接收器被触发的原因并对其采取行动。为此,使用多个接收器就有点矫枉过正了。

希望这对您有所帮助。

关于Android - 避免用户更改日期/时间时触发警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35135340/

相关文章:

jquery - 进行异步 AJAX 调用时如何防止在 Android 中卡住 PhoneGap 应用程序?

android - 如何从服务器播放音频文件 Mp3

java - 如何定期跟踪网络变化

android - Android 6.0 Doze 模式下的 Alarm Manager 问题

java - 如何同步通知生成器和警报管理器?

java - R.java 文件每次都会自动删除

java - 在 Android 6.0 上切换飞行模式

android - 广播接收器在主线程上运行?

android - BroadcastReceiver 不适用于 SMS_RECEIVED

Android 定期获取位置并使用服务发布到服务器