本文整理汇总了Java中com.google.android.gms.location.LocationClient.getTriggeringGeofences方法的典型用法代码示例。如果您正苦于以下问题:Java LocationClient.getTriggeringGeofences方法的具体用法?Java LocationClient.getTriggeringGeofences怎么用?Java LocationClient.getTriggeringGeofences使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.android.gms.location.LocationClient
的用法示例。
在下文中一共展示了LocationClient.getTriggeringGeofences方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onReceive
import com.google.android.gms.location.LocationClient; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(LocationActivity.ACTION_GEOFENCE)) {
return;
}
boolean hasError = LocationClient.hasError(intent);
if (hasError) {
// This is an intent that indicates error.
Log.v(LocationActivity.TAG, "hasError == true");
return;
}
int transition = LocationClient.getGeofenceTransition(intent);
List<Geofence> list = LocationClient.getTriggeringGeofences(intent);
if (transition == -1 || list == null) {
Log.v(LocationActivity.TAG, "list == null OR " + transition);
return;
}
Log.v(LocationActivity.TAG, "geo_fence transition == " + transition);
ArrayList<String> requestIds = new ArrayList<String>();
for (Geofence geoFence : list) {
requestIds.add(geoFence.getRequestId());
}
Bundle bundle = new Bundle();
bundle.putStringArrayList("request_ids", requestIds);
// Create a new intent and set extra arguments which contain the
// request_ids of geofences triggered and corresponding transition.
Intent myIntent = new Intent(context, LocationActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
| Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.putExtra("RECEIVER_STARTED", true);
myIntent.putExtra("geo_fences", bundle);
myIntent.putExtra("transition", transition);
if (LocationActivity.isAppForeground) {
context.startActivity(myIntent);
} else {
// Send a notification when the app is in the background
}
}
示例2: onHandleIntent
import com.google.android.gms.location.LocationClient; //导入方法依赖的package包/类
/**
* Handles incoming intents
* @param intent The Intent sent by Location Services. This Intent is provided
* to Location Services (inside a PendingIntent) when you call addGeofences()
*/
@Override
protected void onHandleIntent(Intent intent) {
// Create a local broadcast Intent
Intent broadcastIntent = new Intent();
// Give it the category for all intents sent by the Intent Service
broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);
// First check for errors
if (LocationClient.hasError(intent)) {
// Get the error code
int errorCode = LocationClient.getErrorCode(intent);
// Get the error message
String errorMessage = LocationServiceErrorMessages.getErrorString(this, errorCode);
// Log the error
Log.e(GeofenceUtils.APPTAG,
getString(R.string.geofence_transition_error_detail, errorMessage)
);
// Set the action and error message for the broadcast intent
broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
.putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);
// Broadcast the error *locally* to other components in this app
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
// If there's no error, get the transition type and create a notification
} else {
// Get the type of transition (entry or exit)
int transition = LocationClient.getGeofenceTransition(intent);
// Test that a valid transition was reported
if (
(transition == Geofence.GEOFENCE_TRANSITION_ENTER)
||
(transition == Geofence.GEOFENCE_TRANSITION_EXIT)
) {
// Post a notification
List<Geofence> geofences = LocationClient.getTriggeringGeofences(intent);
String[] geofenceIds = new String[geofences.size()];
for (int index = 0; index < geofences.size() ; index++) {
geofenceIds[index] = geofences.get(index).getRequestId();
}
String ids = TextUtils.join(GeofenceUtils.GEOFENCE_ID_DELIMITER,geofenceIds);
String transitionType = getTransitionString(transition);
sendNotification(transitionType, ids);
// Log the transition type and a message
Log.d(GeofenceUtils.APPTAG,
getString(
R.string.geofence_transition_notification_title,
transitionType,
ids));
Log.d(GeofenceUtils.APPTAG,
getString(R.string.geofence_transition_notification_text));
// An invalid transition was reported
} else {
// Always log as an error
Log.e(GeofenceUtils.APPTAG,
getString(R.string.geofence_transition_invalid_type, transition));
}
}
}
示例3: onHandleIntent
import com.google.android.gms.location.LocationClient; //导入方法依赖的package包/类
/**
* Handle the intent received by the service.
* @param intent This will be a geofence transition intent.
*/
protected void onHandleIntent(Intent intent) {
// Log.d(LOG_TAG, "onHandleIntentCalled.");
// First check for errors
if (LocationClient.hasError(intent)) {
// Get the error code with a static method
int errorCode = LocationClient.getErrorCode(intent);
// Log the error
Log.e("ReceiveTransitionsIntentService", "Location Services error: " +
Integer.toString(errorCode));
/*
* If there's no error, get the transition type and the IDs
* of the geofence or geofences that triggered the transition
*/
} else {
manageCurrentGeofencesCriticalSection.acquireUninterruptibly();
// Get the type of transition (entry or exit)
int transitionType = LocationClient.getGeofenceTransition(intent);
// Test that a valid transition was reported
if ((transitionType == Geofence.GEOFENCE_TRANSITION_ENTER)
|| (transitionType == Geofence.GEOFENCE_TRANSITION_EXIT)) {
List <Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);
Location triggerLocation = LocationClient.getTriggeringLocation(intent);
if(transitionType == Geofence.GEOFENCE_TRANSITION_ENTER){
addToCurrentGeofences(triggerList);
} else {
removeFromCurrentGeofences(triggerList);
}
String currentGeofenceNames = "Current Geofences:";
for(LocationEnvironmentVariable v: currentGeofences)
currentGeofenceNames += v.getLocationName() + "; ";
manageCurrentGeofencesCriticalSection.release();
startService(BaseService.getServiceIntent(this, null,
BaseService.ACTION_DETECT_ENVIRONMENT));
} else {
// An invalid transition was reported
Log.e("ReceiveTransitionsIntentService", "Geofence transition error: " +
Integer.toString(transitionType));
}
}
}
示例4: onHandleIntent
import com.google.android.gms.location.LocationClient; //导入方法依赖的package包/类
/**
* Handles incoming intents
*
* @param intent The Intent sent by Location Services. This Intent is provided
* to Location Services (inside a PendingIntent) when you call addGeofences()
*/
@Override
protected void onHandleIntent(Intent intent) {
// Create a local broadcast Intent
Intent broadcastIntent = new Intent();
// Give it the category for all intents sent by the Intent Service
broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);
Log.d(TAG, "### ------------------------------------------------------- ###");
Log.d(TAG, "### Geofence onHandleIntent : " + intent);
printExtras(intent.getExtras());
Log.d(TAG, "### ------------------------------------------------------- ###");
// First check for errors
if (LocationClient.hasError(intent)) {
int errorCode = LocationClient.getErrorCode(intent);
String errorMessage = LocationServiceErrorMessages.getErrorString(this, errorCode);
// Log the error
Log.e(TAG, getString(R.string.geofence_transition_error_detail, errorMessage));
// Set the action and error message for the broadcast intent
broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
.putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);
// Broadcast the error *locally* to other components in this app
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
} else {
//Future<Location> location = LastLocationFinder.getLastBestLocation(this, );
// If there's no error, get the transition type and create a notification
// Get the type of transition (entry or exit)
int transition = LocationClient.getGeofenceTransition(intent);
// Test that a valid transition was reported
if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER) || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {
// Post a notification
List<Geofence> geofences = LocationClient.getTriggeringGeofences(intent);
MessageActionEnum transitionType = getTransitionString(transition);
// Send Notification
String[] geofenceIds = extractGeofenceRequestsId(geofences);
sendNotification(transitionType, geofenceIds);
// Log the transition type and a message
Log.d(TAG, "### GeoFence Violation : " + transitionType + " for " + geofences.size() + " geofences (like" + geofences.get(0));
} else {
// An invalid transition was reported
// Always log as an error
Log.e(TAG, "### Geofence transition error. Invalid type " + transition +
" in geofences %2$s");
}
}
}
示例5: onHandleIntent
import com.google.android.gms.location.LocationClient; //导入方法依赖的package包/类
/**
* Handles incoming intents
*
* @param intent
* The Intent sent by Location Services. This Intent is provided
* to Location Services (inside a PendingIntent) when you call
* addGeofences()
*/
@Override
protected void onHandleIntent(final Intent intent) {
if (isCorrectIntent(intent)) {
if (LocationClient.hasError(intent)) {
int errorCode = LocationClient.getErrorCode(intent);
// Get the error message
String errorMessage = LocationServiceErrorMessages.getErrorString(this, errorCode);
Log.d(LOG_TAG, "Error: " + errorMessage);
CommonUtils.showShortToast(this, errorMessage);
} else {
// Get the type of transition (entry or exit)
int transition = LocationClient.getGeofenceTransition(intent);
// Test that a valid transition was reported (just an enter
// transition, uncomment the commented
// code to allow too transition exit
if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
/* || (transition == Geofence.GEOFENCE_TRANSITION_EXIT) */) {
List<Geofence> geofenceList = LocationClient.getTriggeringGeofences(intent);
String[] geofenceIds = new String[geofenceList.size()];
// We can remove this loop since we are using just one id
// (with more this is necessary)
for (int i = 0; i < geofenceIds.length; i++) {
String placeId = geofenceList.get(i).getRequestId();
if (placeId.equals(mPlaceId)) {
geofenceIds[i] = placeId;
Log.d(LOG_TAG, "geofence ID received: " + placeId);
SimpleGeofence geofence = new SimpleGeofenceStore(this).getGeofence(geofenceIds[i]);
if (geofence != null) {
NotificationsManager.getInstance(this).showLocationReminderNotification(geofence);
}
}
}
}
// An invalid transition was reported
else {
Log.e(LOG_TAG, "Geofence transition error: " + Integer.toString(transition));
}
}
}
}
示例6: onReceive
import com.google.android.gms.location.LocationClient; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(LocationActivity.ACTION_GEOFENCE)) {
return;
}
boolean hasError = LocationClient.hasError(intent);
if (hasError) {
// This is an intent that indicates error.
Log.v(LocationActivity.TAG, "hasError == true");
return;
}
int transition = LocationClient.getGeofenceTransition(intent);
List<Geofence> list = LocationClient.getTriggeringGeofences(intent);
if (transition == -1 || list == null) {
Log.v(LocationActivity.TAG, "list == null OR " + transition);
return;
}
Log.v(LocationActivity.TAG, "geo_fence transition == " + transition);
ArrayList<String> requestIds = new ArrayList<String>();
for (Geofence geoFence : list) {
requestIds.add(geoFence.getRequestId());
}
Bundle bundle = new Bundle();
bundle.putStringArrayList("request_ids", requestIds);
// Create a new intent and set extra arguments which contain the
// request_ids of geofences triggered and corresponding transition.
Intent myIntent = new Intent(context, LocationActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
| Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.putExtra("RECEIVER_STARTED", true);
myIntent.putExtra("geo_fences", bundle);
myIntent.putExtra("transition", transition);
if (LocationActivity.isAppForeground) {
context.startActivity(myIntent);
} else {
// Send a notification when the app is in the background
String transitionText = transition == Geofence.GEOFENCE_TRANSITION_ENTER ?
context.getResources().getString(R.string.enter) :
context.getResources().getString(R.string.leave);
Bitmap genFenceBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.geofence);
Notification n = new NotificationCompat.Builder(context)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_notify)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(genFenceBitmap)
.setSummaryText(transitionText))
.setContentText(transitionText)
.setContentIntent(PendingIntent.getActivity(context, 0, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(true).build();
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, n);
}
}