设置

时间:2017-01-14 00:18:01

标签: android android-intent alarmmanager

为了快速总结问题,设置的闹钟在我设置闹钟和第二天某个时间之间消失。我的代码似乎都正常工作,警报设置并在适当的时间关闭。初始警报触发并完成后,将自动设置为第二天。我可以使用adb shell的dumpsys警报进行检查和查看。

更新:我在此处添加了更多日志信息

[14/01/2017 20:57:41] Registering start alarm 1 with Alarm Manager event time: 14/01/2017 21:00:00
[14/01/2017 20:57:41] Registering stop alarm 1001 with Alarm Manager event time: 14/01/2017 21:01:00
[14/01/2017 21:00:00] Receiver has received INTENT_ACTION_ALARM_START and is launching the music player for task #1.
[14/01/2017 21:01:00] Receiver has received INTENT_ACTION_ALARM_STOP and is stopping the music player, and sending a restart request to the MusicService.
[14/01/2017 21:01:00] Restart requested on alarm 1, attempting to add new alarms for tomorrow.
[14/01/2017 21:01:00] Registering start alarm 1 with Alarm Manager event time: 15/01/2017 21:00:00
[14/01/2017 21:01:00] Registering stop alarm 1001 with Alarm Manager event time: 15/01/2017 21:01:00

Batch{43d23730 num=1 start=86684689 end=86684689}:

  RTC_WAKEUP #0: Alarm{438c1740 type 0 org.hudsoft.music}

    type=0 whenElapsed=86684689 when=+23h55m1s357ms window=0 repeatInterval=0 count=0

    operation=PendingIntent{43af9370: PendingIntentRecord{43ddd9a8 org.hudsoft.music broadcastIntent}}

Batch{43d1e348 num=1 start=86744688 end=86744688}:

  RTC_WAKEUP #0: Alarm{438c10d8 type 0 org.hudsoft.music}

    type=0 whenElapsed=86744688 when=+23h56m1s357ms window=0 repeatInterval=0 count=0

    operation=PendingIntent{438ea450: PendingIntentRecord{4393a970 org.hudsoft.music broadcastIntent}}

当我上床睡觉并且第二天醒来并检查adb shell dumpsys输出中奇怪的警报时,它也永远不会执行。 我的问题是没有任何用户交互,警报会在哪里发生?如何设置将持续的警报?该文档声称“警报管理器适用于您希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行”

我目前的设计是,当应用程序打开或接收器收到BOOT事件时,我运行一个IntentService来设置我的所有警报并消失,假设警报管理器将处理 其余的部分。我的接收器应该等待响应,问题是每天晚上我睡觉我醒来,我的报警管理员rtc唤醒已经神秘地消失了。

如果您需要任何其他信息来帮助诊断问题,请告诉我这是我第一篇文章,我一直在努力寻找解决方案大约一周!

在我的接收器中,它会停止音乐播放服务,并在第二天重新启动新的闹钟:

 if (intent.getAction().equals("INTENT_ACTION_ALARM_END")) {
    Intent stopIntent = new Intent(context, MusicPlayer.class);
    Task thisTask = (Task) intent.getSerializableExtra("thisTask");
    stopIntent.putExtra("thisTask", thisTask);
    context.stopService(stopIntent);
    Intent restartAlarmIntent = new Intent(context, MusicService.class);
    restartAlarmIntent.setAction("INTENT_ACTION_ALARM_RESTART");
    restartAlarmIntent.putExtra("thisTask", thisTask);
    context.startService(restartAlarmIntent);
}

以下是我用来构建和启动警报的代码

    Intent myIntent = new Intent(this, MusicReceiver.class);
    myIntent.setAction("INTENT_ACTION_ALARM_START");
    myIntent.putExtra("thisTask", task);
    Log.d("Intent", "Sending start intent now");
    PendingIntent pendingStartIntent = buildAlarmIntent(task, "INTENT_ACTION_ALARM_START");
    setSingleExactAlarm(endTime, pendingStartIntent);

    myIntent = new Intent(this, MusicReceiver.class);
    myIntent.setAction("INTENT_ACTION_ALARM_END");
    myIntent.putExtra("thisTask", task);
    Log.d("Intent", "Sending end intent now");
    PendingIntent pendingStopIntent = buildAlarmIntent(task, "INTENT_ACTION_ALARM_END");
    setSingleExactAlarm(endTime, pendingStopIntent);

public PendingIntent buildAlarmIntent (Task task, String action) {
    PendingIntent pendingIntent;
    Intent myIntent = new Intent(this, MusicReceiver.class);
    myIntent.setAction(action);
    myIntent.putExtra("thisTask", task);
    Integer idNum = task.getId();
    if (action.equals("INTENT_ACTION_ALARM_END")) {
        idNum += 1000;
        pendingIntent = PendingIntent.getBroadcast(this, idNum, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    } else {
        pendingIntent = PendingIntent.getBroadcast(this, idNum, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    return pendingIntent;
}

private void setSingleExactAlarm(long time, PendingIntent pIntent) {
    AlarmManager mAlarmManager;
    mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= 19) {
        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
    } else {
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, pIntent);
    }
}

这是我的AndroidManifest的接收器部分

    <receiver
        android:name=".MusicReceiver"
        android:enabled="true"
        android:exported="true"
        android:process=":remote">
        <intent-filter>
            <action android:name="INTENT_ACTION_ALARM_START" />
            <action android:name="INTENT_ACTION_ALARM_END" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

提前感谢您花时间研究我的问题!

2 个答案:

答案 0 :(得分:1)

我找到了我的问题,我想我会发布它以防其他人遇到类似的问题。我之前设定的警报重新启动。由于时间已经过去,他们会立即解决并消失。这也导致了一个循环,它会尝试一次又一次地重新启动它但是我相信警报管理器意识到这是在事件发生之后并且停止排队它们以防止无限循环重置过去有开始时间的警报。希望这有助于某人!

答案 1 :(得分:0)

我没有足够的声誉发表评论,因为我没有明确的答案。但是,您的手机是否有可能关闭电源?根据Android文档:

https://developer.android.com/reference/android/app/AlarmManager.html

“设备处于睡眠状态时保留已注册的警报(如果设备在此期间关闭,可以选择将设备唤醒),但如果设备关闭并重新启动,则会清除设备。”

如果没有接收BOOT_COMPLETED的代码,很难进一步诊断。

过去我使用以下方法设置闹钟,但我没有您所描述的问题:

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, MyBroadcastReceiver.class);
    PendingIntent pendingIntent  = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pendingIntent);

我希望这会有所帮助,祝你好运。