简体   繁体   中英

How can I detect when a Cordova app has been removed from the recents menu, from within an Android plugin?

This may seem like a strange requirement, so I will just describe what I am trying to do, and why I think I need to know when the task is removed.

Essentially, I am trying to record the location in the background. I found a rough example of how to do so with a BroadcastReceiver , here , and I managed to implement it. I can turn it on and off programatically, from JavaScript.

However, I am having trouble managing when it is turned off. I want to turn it off when the user swipes the main app from the recents menu, but not when the app is destroyed due to low memory.

I can add a check in the BroadcastReceiver detect when the app is no longer running, but I can't detect how it was closed.

Is there any way to detect when the app is closed in this manner?

I found onTaskRemoved() but I am not sure how to use it in this case, or if it would be called after the app is destroyed due to low memory.

It can be done by using a Service.

Simply starting the service with activity.startService(<Intent>) will ensure the service's onTaskRemoved() is called when the activity is removed from the recents menu.

The service can then call a method to stop the background updates. In my case, that is LocationServices.FusedLocationApi.removeLocationUpdates(<GoogleApiClient>, <Intent>); .

Whether the BroadcastReceiver is local to the service, or implemented as a class of its own, does not seem to matter.

A working implementation can be found here .

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