java - 开机后多次报警重启

标签 java android broadcastreceiver alarmmanager reboot

我正在设置多个闹钟,以便它们可以在特定的日子重复。现在我听说 Android 不会在重启时保存警报。我还读到,当 BOOT_COMPLETED 重新安排所有警报时,应使用 BroadcastReceiver

但是,如果我每天有 5 个闹钟 = 在不同的日子安排了大约 35 个闹钟,我该如何告诉 BroadcastReceiver 在重启后重新安排闹钟。我需要将它们存储在数据库中还是?我如何存储它们?还是我只需要 BOOT_COMPLETED?这种事情有什么例子吗?我找不到它。

这是我目前用于设置警报和我的简单接收器类的内容。我在这里使用 Service 而不是 BroadcastReceiver,因为我听说 BR 应该只用于处理短的东西,将来我将不得不使用一些长的声音 fragment 。

   private void setAlarm(){     
            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
            PendingIntent pendingintent = PendingIntent.getService(getBaseContext(), 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);

            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + sveskupa, pendingintent);
            Toast.makeText(getBaseContext(), "Alarm is set", Toast.LENGTH_LONG).show();
        }

报警接收器类:

public class AlarmReceiver extends Service{

            @Override
            public IBinder onBind(Intent intent) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public void onCreate() {
                // TODO Auto-generated method stub
                super.onCreate();
                final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.radio3);
                MPRadio1.start();
                Toast.makeText(getBaseContext(), "OnCreate", Toast.LENGTH_LONG).show();

            }        

最佳答案

But how do I tell the BroadcastReceiver to reschedule alarms after reboot if I have 5 alarms per day = around 35 alarms scheduled on different days.

闹钟计划要么是固定的、不变的并嵌入到您的代码中,要么不是。

如果它嵌入到您的代码中,只需使用启动时 BroadcastReceiver 中的相同代码来重新建立警报计划。

否则,闹钟计划来自某处,因为由于宇宙射线与手机 CPU 和内存的相互作用,闹钟计划不太可能自发创建。您需要确保在重新启动后可以访问相同的信息。

Do I need to store them in the database or? How do I store them?

这取决于你。

I'm using Service instead of BroadcastReceiver here because I have heard that BR should be used to only process short things and in the future I'll have to use some long sound clips.

不要直接从_WAKEUP 样式的闹钟中使用服务,因为无法保证您的服务 code> 将永远得到控制。要么不要使用 _WAKEUP 样式的警报,要么让警报触发 BroadcastReceiver,它可以与 WakeLock 一起使用以确保您的 Service 获得控制权并可以完成其工作。

关于java - 开机后多次报警重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14490871/

相关文章:

java - 尝试将自定义对象写入outputStream时出现java.io.NotSerializedException

java - 是否可以在单个但参数化的测试之前执行一次方法?

android - Android 中 JNI 的内存限制是什么?

android - 如何将系统库添加到 Android Studio?

android - GcmBroadcastReceiver 非法状态异常 : Not allowed to start service Intent

java - 这段代码如何为每个事物对象提供新的 id。 id 应该始终等于 1

java - canvas.drawText - 多次与一次开销

Android studio .XML 与教我 android 开发的书混淆

Android 编程 - 如何从广播接收器播放 SoundPool 声音?

android - BroadcastReceiver 不会对外部 Intent 使用react