本文整理汇总了Java中com.google.android.gms.location.Geofence类的典型用法代码示例。如果您正苦于以下问题:Java Geofence类的具体用法?Java Geofence怎么用?Java Geofence使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Geofence类属于com.google.android.gms.location包,在下文中一共展示了Geofence类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: GeofencingController
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
public GeofencingController(Context context) {
this.context = context;
// Empty list for storing geofences.
mGeofenceList = new ArrayList<Geofence>();
// Initially set the PendingIntent used in addGeofences() and removeGeofences() to null.
mGeofencePendingIntent = null;
// Retrieve an instance of the SharedPreferences object.
mSharedPreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME,
MODE_PRIVATE);
// Get the value of mGeofencesAdded from SharedPreferences. Set to false as a default.
mGeofencesAdded = mSharedPreferences.getBoolean(Constants.GEOFENCES_ADDED_KEY, false);
// Kick off the request to build GoogleApiClient.
buildGoogleApiClient();
}
示例2: getGeofenceTransitionDetails
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
/**
* Gets transition details and returns them as a formatted string.
*
* @param context The app context.
* @param geofenceTransition The ID of the geofence transition.
* @param triggeringGeofences The geofence(s) triggered.
* @return The transition details formatted as String.
*/
private String getGeofenceTransitionDetails(
Context context,
int geofenceTransition,
List<Geofence> triggeringGeofences) {
String geofenceTransitionString = getTransitionString(geofenceTransition);
// Get the Ids of each geofence that was triggered.
ArrayList triggeringGeofencesIdsList = new ArrayList();
for (Geofence geofence : triggeringGeofences) {
triggeringGeofencesIdsList.add(geofence.getRequestId());
}
String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);
return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}
示例3: createGeofencingRequest
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
private GeofencingRequest createGeofencingRequest() {
try {
double longitude = Double.parseDouble(longitudeInput.getText().toString());
double latitude = Double.parseDouble(latitudeInput.getText().toString());
float radius = Float.parseFloat(radiusInput.getText().toString());
Geofence geofence = new Geofence.Builder()
.setRequestId("GEOFENCE")
.setCircularRegion(latitude, longitude, radius)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build();
return new GeofencingRequest.Builder().addGeofence(geofence).build();
} catch (NumberFormatException ex) {
toast("Error parsing input.");
return null;
}
}
示例4: updateGeofencesList
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
/**
* Updates the local {@link ArrayList} of geofences from the data in the passed list
*Uses the place id defined by the API as the geofence object id.
*
* @param places the placeBuffer result of the getPlaceByid call.
*/
public void updateGeofencesList(PlaceBuffer places){
mGeofenceList = new ArrayList<>();
if (places==null || places.getCount()==0) return;
for (Place place: places){
String placeUid = place.getId();
double latitude = place.getLatLng().latitude;
double longitude = place.getLatLng().longitude;
//Build a geofence object
Geofence geofence = new Geofence.Builder()
.setRequestId(placeUid)
.setExpirationDuration(GEOFENCE_TIMEOUT)
.setCircularRegion(latitude,longitude,GEOFENCE_RADIUS)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build();
mGeofenceList.add(geofence);
}
}
示例5: getGeofencingRequest
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
private GeofencingRequest getGeofencingRequest() {
if (this.place == null || this.place.getLocation() == null) {
return null;
}
// called when the transition associated with the Geofence is triggered)
List<Geofence> geoFenceList = new ArrayList<Geofence>();
geoFenceList.add(new Geofence.Builder()
.setRequestId(GEOFENCE_REQUEST_ID)
.setCircularRegion(this.place.getLocation().getLatitude(),
this.place.getLocation().getLongitude(),
GEOFENCE_RADIUS_IN_METERS
)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL | Geofence.GEOFENCE_TRANSITION_ENTER)
.setLoiteringDelay(LOITERING_DELAY_MS)
.setNotificationResponsiveness(NOTIFICATION_RESPONSIVENESS_MS)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build());
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL);
builder.addGeofences(geoFenceList);
return builder.build();
}
示例6: geofenceFromMap
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
private Geofence geofenceFromMap(Map<String, Object> regionData, String regionName) {
Number latitude = (Number) regionData.get("lat");
Number longitude = (Number) regionData.get("lon");
Number radius = (Number) regionData.get("radius");
Number version = (Number) regionData.get("version");
if (latitude == null) {
return null;
}
Builder geofenceBuilder = new Builder();
geofenceBuilder.setCircularRegion(latitude.floatValue(),
longitude.floatValue(), radius.floatValue());
geofenceBuilder.setRequestId(geofenceID(regionName, version.intValue()));
geofenceBuilder.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT);
geofenceBuilder.setExpirationDuration(Geofence.NEVER_EXPIRE);
return geofenceBuilder.build();
}
示例7: onHandleIntent
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
@Override
protected void onHandleIntent(Intent intent) {
try {
GeofencingEvent event = GeofencingEvent.fromIntent(intent);
if (event.hasError()) {
int errorCode = event.getErrorCode();
Log.d("Location Client Error with code: " + errorCode);
} else {
int transitionType = event.getGeofenceTransition();
List<Geofence> triggeredGeofences = event.getTriggeringGeofences();
if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ||
transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
LocationManagerImplementation locationManager = (LocationManagerImplementation)
ActionManager.getLocationManager();
if (locationManager != null) {
locationManager.updateStatusForGeofences(triggeredGeofences, transitionType);
}
}
}
} catch (Throwable t) {
Util.handleException(t);
}
}
示例8: handleLogTransition
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
private void handleLogTransition(int geofenceTransition) {
String transitionStr = "";
switch (geofenceTransition) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
transitionStr = "GEOFENCE_TRANSITION_ENTER";
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
transitionStr = "GEOFENCE_TRANSITION_EXIT";
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
transitionStr = "GEOFENCE_TRANSITION_DWELL";
break;
}
Log.d(TAG, transitionStr + " detected! ");
}
示例9: getGeofenceList
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
private static List<Geofence> getGeofenceList(List<Place> places) {
List<Geofence> geofenceList = new ArrayList<>();
for (Place place : places){
geofenceList.add(new Geofence.Builder()
// Set the request ID of the geofence. This is a string to identify this
// geofence.
.setRequestId(String.valueOf(place.getId()))
.setCircularRegion(
place.getLatitude(),
place.getLongitude(),
place.getRadius()
)
.setExpirationDuration(NEVER_EXPIRE)
.setLoiteringDelay(LOITERING_DWELL_DELAY)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT | Geofence.GEOFENCE_TRANSITION_DWELL)
//.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT | Geofence.GEOFENCE_TRANSITION_DWELL)
.build());
}
return geofenceList;
}
示例10: onHandleIntent
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent fenceEvent = GeofencingEvent.fromIntent(intent);
if (fenceEvent.hasError()) {
String errorMessage = GeofenceStatusCodes.getStatusCodeString(fenceEvent.getErrorCode());
Timber.i(errorMessage);
return;
}
Timber.i("We got a geofence intent");
if (fenceEvent.getGeofenceTransition() != Geofence.GEOFENCE_TRANSITION_DWELL
|| fenceEvent.getTriggeringGeofences().isEmpty()) {
return;
}
String placeId = fenceEvent.getTriggeringGeofences().get(0).getRequestId();
Realm realm = Realm.getDefaultInstance();
Place place = Place.findFirst(realm, placeId);
String title = place != null ?
getString(R.string.geofence_notification_place_title, place.getName()) :
getString(R.string.geofence_notification_title);
String content = getString(R.string.geofence_notification_content);
String imageUrl = place != null ? place.getFirstImage(GEOFENCE_LARGE_ICON_SIZE) : null;
realm.close();
sendNotification(title, content, placeId, imageUrl);
}
示例11: getTransitionDetails
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
/**
* Concatenates all the id's to one String
* @param context from which a call is being made
* @param geofenceTransition type of transition
* @param triggeringGeofences list of all triggering transitions
* @return list of Strings with the type of a transition and all triggering id's
*/
public static List<String> getTransitionDetails(Context context,
int geofenceTransition,
List<Geofence> triggeringGeofences) {
String geofenceTransitionString = getTransitionString(context, geofenceTransition);
// Get the Ids of each geofence that was triggered.
List<String> triggeringGeofencesIdsList = new ArrayList<>();
triggeringGeofencesIdsList.add(geofenceTransitionString);
for (Geofence geofence : triggeringGeofences) {
triggeringGeofencesIdsList.add(geofence.getRequestId());
}
return triggeringGeofencesIdsList;
}
示例12: onReceive
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
GeofencingEvent event = GeofencingEvent.fromIntent(intent);
if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER)
Log.d("aken-ajalukku", "Entered geofence!");
else
Log.d("aken-ajalukku", "Exited geofence!");
if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER) {
List<Geofence> geofences = event.getTriggeringGeofences();
ArrayList<PointOfInterest> pois = new ArrayList<>();
for (Geofence gf : geofences) {
pois.add(Data.instance.getPoiById(Integer.parseInt(gf.getRequestId())));
}
createNotification(context, pois);
}
}
示例13: populateGeofencesList
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
private void populateGeofencesList() {
geofences = new ArrayList<>();
for (PointOfInterest poi : Data.instance.getPois()) {
geofences.add(
new Geofence.Builder()
.setRequestId(String.valueOf(poi.getId()))
.setCircularRegion(poi.getLocation().latitude,
poi.getLocation().longitude,
GEOFENCE_RADIUS_IN_METERS)
.setExpirationDuration(GEOFENCE_EXPIRATION_IN_MILLISECONDS)
.setNotificationResponsiveness(GEOFENCE_RESPONSIVENESS_IN_MILLISECONDS)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build()
);
}
}
示例14: toGeofence
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
public Geofence toGeofence(Date expiryDate) {
Long expirationDurationMillis = 0L;
if (expiryDate != null) {
expirationDurationMillis = expiryDate.getTime() - Time.now();
}
if (expirationDurationMillis <= 0) {
expirationDurationMillis = Geofence.NEVER_EXPIRE;
}
return new Geofence.Builder()
.setCircularRegion(getLatitude(), getLongitude(), getRadius())
.setRequestId(getId())
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setExpirationDuration(expirationDurationMillis)
.build();
}
示例15: shouldCalculateRefreshDatesForGeoStartAndExpired
import com.google.android.gms.location.Geofence; //导入依赖的package包/类
@Test
public void shouldCalculateRefreshDatesForGeoStartAndExpired() throws Exception {
// Given
Long millis15MinAfterNow = now + 15 * 60 * 1000;
Long millis30MinAfterNow = now + 30 * 60 * 1000;
String date15MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis15MinAfterNow));
String date30MinAfterNow = DateTimeUtil.ISO8601DateToString(new Date(millis30MinAfterNow));
saveGeoMessageToDb(date15MinAfterNow, date30MinAfterNow);
// When
Pair<List<Geofence>, Pair<Date, Date>> geofencesAndNextRefreshDate = geofencingImpl.calculateGeofencesToMonitorDates(geoStore);
// Then
assertNotNull(geofencesAndNextRefreshDate);
assertTrue(geofencesAndNextRefreshDate.first.isEmpty());
assertNotNull(geofencesAndNextRefreshDate.second);
Date refreshStartDate = geofencesAndNextRefreshDate.second.first;
Date refreshExpiryDate = geofencesAndNextRefreshDate.second.second;
assertEquals(millis15MinAfterNow, refreshStartDate.getTime(), 3000);
assertEquals(millis30MinAfterNow, refreshExpiryDate.getTime(), 3000);
}