Android Alarm Manager在重新启动时不会在特定时间触发

时间:2016-01-07 07:52:55

标签: android

我遇到了AlarmManager的问题。当设备重新启动时,即使指定的时间不正确,它也会自动触发警报(例如,设备在下午3:00完全重启,指定的时间是凌晨4:00)

这是我到目前为止所尝试的内容:

class DeviceBootReceiver extends BroadcastReceiver {

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

    // Check if successful reboot
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {

        SharedPreferences shared = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
        // Check if daily notification is enabled
        // If yes, call NotificationPublisher to communicate with NotificationAlarmService
        if(shared.getBoolean(Constants.KEY_IS_DAILY_NOTIFIED, false)) {
            DBHelper dbHelper = new DBHelper(context);
            DBConnector.dbConnect(dbHelper);

            int DAILY_NOTIFICATION_ID = Constants.DAILY_VERSE_NOTIFICATION_1_ID;
            ArrayList<Notification> notificationList = dbHelper.getNotifications();
            Log.e("", "notificationList: " + notificationList.size());

            for(Notification obj : notificationList) {
                Calendar datetime = Calendar.getInstance();
                datetime.set(Calendar.HOUR_OF_DAY, obj.getHourOfDay());
                datetime.set(Calendar.MINUTE, obj.getMinute());
                datetime.set(Calendar.SECOND, 0);

                Calendar now = Calendar.getInstance();
                if (now.after(datetime)) {
                    datetime.add(Calendar.DATE, 1);
                }

                Log.e("BOOT RECEIVER", "" + obj.getHourOfDay() + ":" + obj.getMinute());

                Intent myIntent = new Intent(context, NotificationPublisher.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                        DAILY_NOTIFICATION_ID++,
                        myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        datetime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                        pendingIntent);
            }

        }

    }
}

AndroidManifest:

<receiver
        android:name=".utils.DeviceBootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

我的代码似乎有什么问题?我很感激任何帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

Docs说:

  

如果规定的触发时间是过去的,则会立即触发警报。

因此,您应该检查是否在应用逻辑中设置了过去的时间。

例如,在您设置闹钟的BOOT_COMPLETED接收器中,您可以检查它是否是有效时间。另一种选择是在你的目标组件被触发,你可以检查这个。

答案 1 :(得分:0)

如果您设置了过去时间的闹钟,它将立即触发。您必须在设置闹钟之前手动检查。

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            // Set alarms
            Calendar calendar = Calendar.getInstance();


            calendar.set(Calendar.HOUR_OF_DAY, 18);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            Intent intent1 = new Intent(context, AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getService(
                    context, 0, intent1,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

            long triggerMillis = calendar.getTimeInMillis();

            if (calendar.getTimeInMillis() < Calendar.getInstance()
                    .getTimeInMillis()) {
                triggerMillis = calendar.getTimeInMillis() + INTERVAL_DAY;

                System.out.println("Alarm will go off next day");
            }
       #Set alarm here using "triggerMillis"
        .........................
         ...........................
}