Reputation: 545
I am working on a location-based google glass app which gives details detecting user's location. I want my app to start automatically only when the user is at a certain position without his/her input.
For example, a google glass user wants to take the metro. The app should automatically start as soon as the user enters the metro station and provide him/her the necessary details(like Google now cards pop up).
Is there anyway this can be implemented?
Upvotes: 1
Views: 135
Reputation: 221
You can do that with LocationManager and using addProximityAlert which sets a proximity alert for the location given by the position (latitude, longitude) and the given radius.
addProximityAlert - from developer.android.com:
public void addProximityAlert (double latitude, double longitude, float radius, long expiration, PendingIntent intent)
Added in API level 1 Set a proximity alert for the location given by the position (latitude, longitude) and the given radius.
When the device detects that it has entered or exited the area surrounding the location, the given PendingIntent will be used to create an Intent to be fired.
The fired Intent will have a boolean extra added with key KEY_PROXIMITY_ENTERING. If the value is true, the device is entering the proximity region; if false, it is exiting.
Due to the approximate nature of position estimation, if the device passes through the given area briefly, it is possible that no Intent will be fired. Similarly, an Intent could be fired if the device passes very close to the given area but does not actually enter it.
After the number of milliseconds given by the expiration parameter, the location manager will delete this proximity alert and no longer monitor it. A value of -1 indicates that there should be no expiration time.
Internally, this method uses both NETWORK_PROVIDER and GPS_PROVIDER.
Before API version 17, this method could be used with ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION. From API version 17 and onwards, this method requires ACCESS_FINE_LOCATION permission.
Parameters latitude the latitude of the central point of the alert region longitude the longitude of the central point of the alert region radius the radius of the central point of the alert region, in meters expiration time for this proximity alert, in milliseconds, or -1 to indicate no expiration intent a PendingIntent that will be used to generate an Intent to fire when entry to or exit from the alert region is detected
Throws SecurityException if ACCESS_FINE_LOCATION permission is not present
Upvotes: 2