Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android geofence with mock location provider

I have started developing on Android with last Location services feature : Geofences !! Is there any known problem with mock location provider ? Following example here (https://developer.android.com/training/location/geofencing.html) my intent service never fired even if the current location is inside geofence. I'm using FakeGPS android app as mock location provider and if I simulate a route I see the location changes on Google Maps app, so the mock location provider is working well. Any ideas ?

Thanks. Paolo.

like image 316
ppatierno Avatar asked Jun 11 '13 16:06

ppatierno


People also ask

Can you geofence without an app?

An app – very important to note that geofences for 99,9% of the use cases cannot be used without an app. There are some minor exceptions, but most likely, if you are reading this blog post, you will need an application – whether your own or through a partner – that can be utilized for this purpose.

Is geofencing API free?

So, why shouldn't we use the Geofencing API? you might ask. The API uses a pay-as-you-go pricing model. After you consume all the initial free requests, the charges are 5.00 USD per 1000 hits.

Can I do my own geofencing?

Building your own geofences might seem like an easy task, but it's important to understand the many challenges involved. While it's possible for developers and product owners to set up geofencing in their apps, many companies come to the best geofencing software providers such as PlotProjects to complete the process.

How do I geofence a location?

How geofencing works. To make use of geofencing, an administrator or developer must first establish a virtual boundary around a specified location in GPS- or RFID-enabled software. This can be as simple as a circle drawn 100 feet around a location on Google Maps, as specified using APIs when developing a mobile app.


3 Answers

I tried forever to get this to work. What a pain Google! Since it says geofences can easily be tested using mocks.

The magic trick is to use the provider name "network" in the Location passed to setMockLocation.

    Location location = new Location("network");
    location.setLatitude(latitude);
    location.setLongitude(longitude);
    location.setTime(new Date().getTime());
    location.setAccuracy(3.0f);
    location.setElapsedRealtimeNanos(System.nanoTime());

    LocationServices.FusedLocationApi.setMockLocation(_googleApiClient, location);
like image 144
Eric L Avatar answered Sep 21 '22 09:09

Eric L


Actually Intent service used in the mentioned example works good if your app is in foreground but when the app is in background, this IntentService is never called.So we need to use Broadcast-Receiver instead of Intent service.

I found this blog helpful in getting solution.

http://davehiren.blogspot.in/2015/01/android-geofence-stop-getting.html

like image 32
Akash Bisariya Avatar answered Sep 23 '22 09:09

Akash Bisariya


Geofences use FusedLocationProviderApi so to mock them you have to use FusedLocationProviderApi.setMockLocation

like image 33
Marian Paździoch Avatar answered Sep 22 '22 09:09

Marian Paździoch