Detecting geofence entrence using new API while the app is off - android

Just watched the session about new location capabilities:
https://developers.google.com/events/io/sessions/325337477
and think that feature of GEOFENCES will be a good fit for my app.
I also downloaded the demo app which while quite informative is not giving me the functionality I am looking for.
So could someone please explain me or point me to some example on how could I add a geofencing detection to the app that works while the app is off. If you have seen the IO session and noticed the app Zillo that is what I am talking about.
What I know by now is that I don't want to use a constantly running service in the background that detects TRANSITION broadcast but would like to register for them in the manifest file and than handle them inside a broadcast receiver class. Something like you would do when using GCM messaging.
So the questions are:
What broadcast needs to be registered in the manifest?
When should I register my fences so they are registered only once and stay registered when the app is closed?
Are there any pointers on how to optimize battery life in a case like this?
If someone already has experience what radius for a geofence would you suggest to detect entering a large building (3000-5000 sqm ground floor area for example)?

I implemented what you are talking about. (using a broadcast receiver instead of a IntentService). I started with the sample code, then made a few small changes, listed in my response here.
It seems to be working fine when the app is closed.
As far as optimizing battery life, this is all done by the OS. You just register the fences and Android handles the rest. I think they have optimized it well, I haven't seen significant drain after registering almost 100 fences. But I haven't done extensive testing.
As far as the radius size, I suggest building a sample app that has a map in it, and put a pin on the lat lng and play with the radius till it looks right. For a wild guess, I'd suggest something around 150 meter radius?
--update--
More detail on what I've found as far as accuracy. I built a testing app to see track myself and see the accuracy, and been driving around for the last few weeks and checking it frequently. I've only been testing with gps off since thats similar to what most users will encounter when they are out and about. :
Outdoors, not close to any wifi signals: Not very accurate, something like 100-200+ meters
Outdoors, close to wifi signals: Surprisingly accurate. Between 10-30 meters. If I'm sitting outside a restaurant, or at a strip mall where there are wifi signals, it knows very well where I am.
Indoors: even with wifi, many times it seemed less accurate than outdoors. More like 30-50 meters.

Related

Android Location GPS Track

I have a small test App that with an Android GPS API map fragment. I use FusedLocationProvider. TarketSDK=29. Using Java.
As long as the app is active it works beautifully. On locationUpdates, I add a new point to the track and everything looks great and stays accurate. The goal is to track my hike, total distance and track and show it on the map. Works great.
As soon I lock my phone or loses focus, then the updates stop and I no longer get location updates.
Solution seems to be:
Background Service (discouraged)
Foreground Service
PendingIntent
I have poured over the docs, StackOverflow, all examples/tutorials I can find, developer.android.com, etc. I have downloaded examples of the latter 2 from GitHub; they seem incredibly obtuse (probably just me).
What are the dis/advantages of ForegroundService vs PendingIntent?
How about a bare-bones example illustrating the min features of each to implement location updates while your phone is locked in your pocket or some other app is active? Just the template minimum.
I need to save the locationUpdates that occur while my app is not active or phone is locked; in order to fill in Track when activity is restored to the app.
Some simple end-to-end guidance from my working app to something that will maintain locationUpdates and save the data would be great.
Ok - I have answered my question in a roundabout way.
I had been Searching on "retrieving location updates when app is not active". This lead to the various solutions of background service, foreground service, pendingIntents, etc.
I eventually found that if you just start a Foreground Service with a Notification, even if your phone is locked or you switch active apps, your App continues to receive LocationUpdates; as the Foreground Service runs in the same thread and therefore activates your app code (if I understand the reasons why correctly).
So, I started searching on just how to start a Foreground Service. As anyone knows that has tried to figure this out lately, this has changed more than a couple times over recent versions. The online docs at developer.android.com are not up to date. You will spend a lot of time wondering why things do not work following these docs.
Eventually, with just searching on how to start a foreground service, I came across this simple and straightforward (non-youtube-video - don't you just hate those things) tutorial. https://androidwave.com/foreground-service-android-example/
I just added this code to my existing Mapping code that works when the app is active, and tested with locking the phone and putting it in my pocket and switching apps and doing the same. It appears to solve the problem.
Update: Added code to count number of location updates and average accuracy of each update holding the phone in hand, screen on and app active as the baseline. Phone locked, or App not active no difference in number of updates nor accuracy. Phone locked and in pocket, no difference in number of updates, but accuracy suffered by from an average of 10m to an average of 13m; to be expected I assume whilst in the pocket.

Foreground or Background service to track user's location?

I'm developing a location aware app. This app will start tracking users when they are in their workday. These are the requirements:
The service should run on a regular basis (every 30 or 45 min).
It won't matter if the service does not trigger at the same basis everytime.
The service needs to upload data to our firestore db.
I doesn't need to be triggered on specific conditions (data is on, phone is charging, etc...)
I need this to run even if the user restarts his phone.
We may need to track the distance traveled by the user. (This is not a requirement per se, but it may be a feature someday)
I know there are a lot of ways to achieve this, but I have not decided which is the option that best fits my scenario. This is what I've thought so far:
Foreground service combined with BroadcastReciever in case the phone is rebooted
Background service using the new jetpack's Workmanager.
Which will the best solution? Do you think there is a better solution than those?
Thanks!
Was thinking create a GPS location tracker so when they are in work premise as well as outside it kinda shows.
Then consider adding the number 5 of the above. Like you said there could be mire awesome solutions than these so lets wait for options.

What is the most battery-efficient way of monitoring proximity to a number of locations in Android?

I have an app which will take some (internet) action every time the user approaches any one of a set of locations (for example, let's say it notifies a server whenever I am near a Starbucks shop, so my coffee habits can be analysed).
What's the most battery-efficient (yet reliable and relatively accurate) way of causing the phone to do something whenever it's at a certain location? I don't need perfect location accuracy - within 50m or so is fine.
Currently, I set a ProximityAlert for each location with a 100m radius, and have it raise an Intent, which is then served by a Service which runs in the background. The service also includes an Alarm which causes a download every three hours or so.
The Service itself doesn't appear to drain any significant amount of battery - the battery is all drained by "Android OS". According to the Android 4.1.2 Battery monitor, Android OS is using 3x as much CPU as a phone without my app, and has "stay awake" on for most of the time.
Would changing it to an IntentService make a difference?
Would reducing (or increasing) the radius of the alert make a difference?
Would converting it to a Geofence (https://developer.android.com/training/location/geofencing.html) make much difference?
Can I set it to use a cheaper location service unless it thinks it's close to one of the locations (or will it do that automatically?)
I have never worked with the LocationManagers proximity alert functions, so I do not really know how much they drain the battery, but as far I know the new Geofence services (https://developer.android.com/training/location/geofencing.html) were specially designed in a battery-friendly fashion.
LocationManagers pose a very difficult way to program something that you need. Google has reinvented its location providers to fuse them with sensors. Its the latest and best, uses less battery and does magic with your application, by removing all the 1000's of lines of useless code that you need to write between your gps / network providers.
The new api's are LocationClient api's. Watch this video and demo for yourself.
you can use new geo fence api provided by the Google recently,its more power efficient
https://developer.android.com/training/location/geofencing.html

Android service location code?

I've been looking everywhere for an example of code that is a Service that update a web server on the users location. this is made almost impossible because of the use of the word 'service' as it is used in the context of location service, for example 'googles location service'. please can anyone point me in the direction of an example where a SERVICE gets the location and does something with it. I can send it to a web service so I'm not asking for that but I just want to know if there is a way of every certain amount of time and distance a service will update the web server?
Look at Android docs about obtaining user location.
You will have to make several decisions:
How accurate do you need your updates to be?
How often do you need them?
This greatly affects device's power consumption. Personally I loathe the apps that prevent full sleep and have GPS constantly turned on - this drains the battery in a couple of hours.
I highly recommend you look at this blog post: A Deep Dive Into Location. There is a link to a great example app.

Detect when android device is in a moving car

For a personal project, I'm trying to detect when an android device is with someone driving. I initially thought of using GPS to determine average velocity, but decided that would consume power and requires that the user has GPS enabled at all times. I'd heard that it's possible to tell when someone switches cell towers (on both android and iOS), but is there a broadcast intent for this? I'm very new to Android development, so was hoping I could register a broadcast receiver to run when I believe the device is in a moving car.
I'm not sure if I can create my own broadcast intents and I assume I couldn't rely on a background process to always be running since android may kill it if memory is needed.
As a side note, I notice that some apps, like skype, have a background process that restarts itself even if I force kill it. How do they do this? My thought would be to have a broadcast receiver for when device moves, turns on, etc. to restart the background process... is that what they do?
Thanks for any info!
I would suggest GPS would give you much better indication of motion, since you likely change towers somewhat infrequently. Even mapping applications that are able to triangulate your position using tower signal are extremely inaccurate. Often even with a very good signal they aren't able to triangulate your position any better than within 1600m.
You could also look into asking the user if they have a bluetooth kit in their car. If they have you can find the name of this pairing and when connected you know they are in the car (or close to it).

Categories

Resources