Android setInexactRepeating不会重复触发

时间:2018-06-28 05:33:54

标签: android notifications alarmmanager

我是Android新手,刚刚开始学习。对于我正在创建的应用程序,我希望包含一个每日通知,该通知每天在指定时间(例如9:00 AM)显示。我在下面给出了实现方案。

在主要活动中:

NotificationHelper.scheduleRepeatingRTCNotification(this, 9, 0);

NotificationHelper类

public class NotificationHelper {

    private static AlarmManager alarmManager;
    private static PendingIntent alarmIntent;
    public static int ALARM_TYPE_RTC = 100;

    public static void scheduleRepeatingRTCNotification(Context context, int hour, int mins) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, mins);

        Intent intent = new Intent(context, AlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

    }
// ... Some other code
}

AlaramReceiver类

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intentToRepeat = new Intent(context, AnotherActivity.class);
        intentToRepeat.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, NotificationHelper.ALARM_TYPE_RTC, intentToRepeat, PendingIntent.FLAG_UPDATE_CURRENT);
        buildNotification(context, pendingIntent);
    }
... // Some other code and shows a notification.
}

清单

<receiver android:name=".notification.AlarmReceiver" />
<receiver
    android:name=".notification.AlarmBootReceiver"
    android:enabled="true">
    <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />                
    </intent-filter>
</receiver>

我的问题是,警报根本没有触发。但是当我在警报时间(例如9:00 AM)之后打开应用程序时,将触发并显示通知。但是不是在手机进入睡眠状态之前还是

预先感谢您的建议。如果这个问题在重复,请原谅我。我找不到其他问题的解决方案,所以我决定写一个新问题。

0 个答案:

没有答案