alarmmanager在一段时间后被取消

56lgkhnf  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(320)

我正在编写我的第一个应用程序,我试图在每天的特定时间触发一个通知,报告一些信息。对于starter,我创建了下面的类,用于在重新启动后重新启动警报,并从主应用程序启动警报(这只需要一段代码即可重用)。

public class SetAlarmReceiver extends BroadcastReceiver {

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

    public void checkServiceRunning(Context context) {
        Intent reminderIntent = new Intent(context, AlarmReceiver.class);
        reminderIntent.setAction("MyReminder");
        // I check if a reminder is already active
        boolean reminderActive = (PendingIntent.getBroadcast(context, REMINDER_SERVICE,
                reminderIntent,
                PendingIntent.FLAG_NO_CREATE) != null);
        Log.d("test", "The reminder is active?" + reminderActive);
        if (!reminderActive) {
            startReminderService(context);
        }
    }

    public void startReminderService(Context context) {
        Calendar serviceNotificationTime = Calendar.getInstance();
        serviceNotificationTime.set(Calendar.HOUR_OF_DAY, 9);
        serviceNotificationTime.set(Calendar.MINUTE, 0);
        serviceNotificationTime.set(Calendar.SECOND, 0);
        Intent reminderIntent = new Intent(context, AlarmReceiver.class);
        reminderIntent.setAction("MyReminder");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
            context,
            REMINDER_SERVICE,
            reminderIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager serviceAlarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        serviceAlarmManager.setRepeating(
            AlarmManager.RTC_WAKEUP,
            serviceNotificationTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY,
            pendingIntent);
        if (pendingIntent != null) {
            Log.d("test", "Background service set up and running.");
        } else {
            Log.d("test", "Failed to set up background service!");
        }
    }
}

之后,我用以下行更新了清单:

<receiver
        android:name="com.mycompany.myapp.services.SetAlarmReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.mycompany.myapp.services.AlarmReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="MyReminder" />
        </intent-filter>
    </receiver>

到目前为止,一切正常。当手机重新启动时,提醒会正确重新启动(如果提醒时间已过),并立即触发。
alarmreceiver类如下:

public class AlarmReceiver extends BroadcastReceiver {

    private Context ctx;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.ctx = context;
        remindStuff();
    }

    private void remindStuff() {
        ....
        some code
        ....

        // I check if the alarm is still on (probably awful, since it just fired)
        SetAlarmReceiver setAlarmReceiver = new SetAlarmReceiver();
        setAlarmReceiver.checkServiceRunning(ctx);
    }
}

我使用相同的方法来检查主活动的警报是否开启:

private void checkServicesRunning() {
    SetAlarmReceiver setAlarmReceiver = new SetAlarmReceiver();
    setAlarmReceiver.checkServiceRunning(getApplicationContext());
}

在我看来一切都是正确的,我已经尝试了我找到的任何解决方案,在stackoverflow上阅读了大量类似我的问题,但我仍然无法理解我做错了什么。系统触发启动后,警报会立即正确触发,当我刚刚启用警报时,警报会正确触发(仅当通知时间是在过去,即晚上10点,警报必须在上午9点触发时)。当checkservicerunning方法运行时,它告诉我警报已经启用(在logcat中返回true),但是当我关闭应用程序并在一段时间后重新运行它时,警报看起来好像被取消了,并且正在由应用程序重新创建。
有什么提示吗?
谢谢。
编辑:我尝试了命令'adb shell dumpsys alarm',它实际上显示我的警报运行正常,所以,在这一点上,我认为问题是其他的,但我不能理解什么。。。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题