简体   繁体   中英

Android ProximityAlert does not stay active after exiting app (i.e. pressing home button)

I am able to set a proximity alert (ie generates a notification when the phone enters/exits specified radius) which works as I want only when I still have the app up. When I click home, the radius exit/enter conditions which normally fire the alert do not result in a notification.

I am pretty sure it has something to do with registering and unregister receivers, but I cannot figure it out. When I don't include a call to unregisterReceiver and press home, a leaked receiver error appears. When I include it in onPause() and click home, I get no errors, but the alert doesn't work.

Is there something I am not understanding about these proximity alerts? What is the best way to have an app respond to proximity as I wish it to?

Register your BroadcastReceiver in the Manifest file. It will automatically register and unregister itself.

<receiver android:name="ProximityIntentReceiver">
    <intent-filter>
        <action android:name="com.example.googlemaps.PROXIMITY_ALERT">
    </intent-filter>
</receiver> 

It will work even after the home button is pressed.

Register pending intent instead of receiver. They survive application shutdown

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM