Proximity Alerts not working after phone reboot

2019-08-12 16:53发布

问题:

I am using proximity alerts in one of my applications, however it seems that whenever I reset my phone (via battery pull) or just in general, the proximity alerts are no longer active.

The only way they work once again is if I uncheck and recheck the checkbox (which removes, then re-adds the proximity alert)

Any idea's or reason?

回答1:

You must reset the alerts. The Android OS does not persist your alerts when rebooting, that's up to your app. Create a BroadcastReceiver (I called mine BootReceiver in my example below) to handle the "android.intent.action.BOOT_COMPLETED" action (this is defined in the manifest). With the BroadcastReceiver you can then restart all of your alerts. Don't forget to add the "android.permission.RECEIVE_BOOT_COMPLETED" permission.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<receiver android:name=".BootReceiver">
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>