AlarmManager当手机处于关机状态 - ANDROID状态、手机、AlarmManager、ANDROID

2023-09-04 07:37:40 作者:微微一笑〞醉傾城

我在做一个报警系统,但我已经当手机处于关闭状态。该报警器不工作的问题。

I'm doing an alarm System but i've a problem when the phone is turned off.. The alarm doesn't work..

我设置去报警如下:

    public void doIntents(Context context, long milis, Tratam trat){
    cal=Calendar.getInstance();
    alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);

    cal.setTimeInMillis(milis);
    Intent intent = new Intent(context, OnAlarmReceiver.class);


    pendingIntent = PendingIntent.getBroadcast(context, trat.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP,milis ,pendingIntent);

}

报警工作正常时,电话被接通。

The Alarm works Ok when the phone is turned on..

我该怎么办?

感谢您!

推荐答案

是的,问题是你的应用程序没有运行在手机重新启动时。您需要注册,以便它收到一个消息的时候,手机重启可以接收BOOT_COMPLETED消息一个BroadcastReceiver。然后在BroadcastReceiver的,你可以重新安排这些报警或什么的。但我不认为有什么你可以做使你触发报警时,手机处于关闭状态。(如:使得手机开启)

Yea, the problem is your app isn't running when the phone restarts. You'll need to register a BroadcastReceiver that can receive the BOOT_COMPLETED message so it receives a message when the phone reboots. Then in the BroadcastReceiver you can either reschedule those alarms or whatever. But I don't think there's anything you can do about making your alarm trigger when the phone is off..(e.g. making the phone turn on)

<receiver android:name="MyBootReceiver"
        android:enabled="true"
        android:exported="true"
        android:label="BootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>

        </intent-filter>
    </receiver>