Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 报警管理器是否在重新启动后仍然存在?_Android_Alarmmanager_Android Alarms - Fatal编程技术网

Android 报警管理器是否在重新启动后仍然存在?

Android 报警管理器是否在重新启动后仍然存在?,android,alarmmanager,android-alarms,Android,Alarmmanager,Android Alarms,我对安卓真的很陌生,我一直在研究警报。我想提醒你那天是否有生日。我用过闹钟管理器。我很困惑,因为我读到它会在重启后清除。我没有安卓手机,所以我只使用模拟器 这是我的密码: public void schedAlarm() { AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); Intent intent = new Intent(this, AlarmService.class); pendi

我对安卓真的很陌生,我一直在研究警报。我想提醒你那天是否有生日。我用过闹钟管理器。我很困惑,因为我读到它会在重启后清除。我没有安卓手机,所以我只使用模拟器

这是我的密码:

public void schedAlarm() {
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmService.class);
    pendingIntent = PendingIntent.getBroadcast(this, contact.id, intent, PendingIntent.FLAG_ONE_SHOT);
    am.setRepeating(AlarmManager.RTC, timetoAlarm, nextalarm, pendingIntent);
}
我做了这个广播接收器来代替AlarmSerivce 在这里:


这就足够了吗?

一个简单的答案是否定的。但是,您可以通过创建一个
广播接收器来实现这一点,该接收器将在设备启动完成时启动警报

使用
捕获BroadCastReceiver类中的启动活动

您需要在AndroidManifest.xml中添加上述行,如下所示

<receiver android:name=".AutoStartUp" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
     <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    </receiver>

是的,即使在重新启动后,您也可以使AlarmManager工作。 也许这是最简单的方法:在AndroidManifest.xml中添加以下代码:

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

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

别忘了将对AndroidManifest.xml的用户权限包括为:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

在某些手机中,仅添加

<action android:name="android.intent.action.Boot_COMPLETED" />

不工作,你也必须添加

<action android:name="android.intent.action.QUICKBOOT_POWERON" />


除了上一个

之外,我可以将AlarmService更改为BroadcastReceiver吗?@Lucifer-重新启动后,Android是否会清除我们设置的所有警报?如果是,我们应该在重新启动后设置它们@Behzad,如果警报是通过代码设置的,则肯定会在重新启动时清除。请注意,您还需要在清单中请求RECEIVE_BOOT_COMPLETED权限。我是否需要在收到启动事件后在此接收器中再次“重新注册”所有警报?即使警报在一小时后设置,它也会在重新启动后自动触发。是,通常,这将在
pendingent
之后调用,后者将在设定的时间段后调用
.AlarmReceiver
。通过添加
BOOT\u COMPLETED
QUICKBOOT\u POWERON
。在上面的回答中,
.AlarmReceiver
在引导时被调用(不正确的操作),并且
PendingEvent
在引导后仍然丢失。相反,应该从
BOOT_COMPLETED
QUICKBOOT_POWERON
调用一个额外的接收器,重新初始化后一个接收器的
pendingent
文档?在developer.android.com上找不到任何东西…默认情况下,当设备关闭时,所有警报都会取消。
<action android:name="android.intent.action.QUICKBOOT_POWERON" />