RESOLU | Définir le stockage de GeofenceBonjour Savez-vous comment définir le stockage de Geofence sous Android? |
Android | |
Geofence Android | |
Maëlys Posté le : 15 Novembre à 09:48 23 733 vues |

Ethan Posté le : 15 Novembre à 09:51 |
Bonjour Voici comment définir le stockage de Geofence sous Android:
public class MainActivity extends FragmentActivity {
...
public class SimpleGeofence {
// Instance variables
private final String mId;
private final double mLatitude;
private final double mLongitude;
private final float mRadius;
private long mExpirationDuration;
private int mTransitionType;
public SimpleGeofence(
String geofenceId,
double latitude,
double longitude,
float radius,
long expiration,
int transition) {
// Set the instance fields from the constructor
this.mId = geofenceId;
this.mLatitude = latitude;
this.mLongitude = longitude;
this.mRadius = radius;
this.mExpirationDuration = expiration;
this.mTransitionType = transition;
}
// Instance field getters
public String getId() {
return mId;
}
public double getLatitude() {
return mLatitude;
}
public double getLongitude() {
return mLongitude;
}
public float getRadius() {
return mRadius;
}
public long getExpirationDuration() {
return mExpirationDuration;
}
public int getTransitionType() {
return mTransitionType;
}
public Geofence toGeofence() {
// Build a new Geofence object
return new Geofence.Builder()
.setRequestId(getId())
.setTransitionTypes(mTransitionType)
.setCircularRegion(
getLatitude(), getLongitude(), getRadius())
.setExpirationDuration(mExpirationDuration)
.build();
}
}
...
public class SimpleGeofenceStore {
// Keys for flattened geofences stored in SharedPreferences
public static final String KEY_LATITUDE =
"com.example.android.geofence.KEY_LATITUDE";
public static final String KEY_LONGITUDE =
"com.example.android.geofence.KEY_LONGITUDE";
public static final String KEY_RADIUS =
"com.example.android.geofence.KEY_RADIUS";
public static final String KEY_EXPIRATION_DURATION =
"com.example.android.geofence.KEY_EXPIRATION_DURATION";
public static final String KEY_TRANSITION_TYPE =
"com.example.android.geofence.KEY_TRANSITION_TYPE";
// The prefix for flattened geofence keys
public static final String KEY_PREFIX =
"com.example.android.geofence.KEY";
public static final long INVALID_LONG_VALUE = -999l;
public static final float INVALID_FLOAT_VALUE = -999.0f;
public static final int INVALID_INT_VALUE = -999;
// The SharedPreferences object in which geofences are stored
private final SharedPreferences mPrefs;
// The name of the SharedPreferences
private static final String SHARED_PREFERENCES =
"SharedPreferences";
// Create the SharedPreferences storage with private access only
public SimpleGeofenceStore(Context context) {
mPrefs =
context.getSharedPreferences(
SHARED_PREFERENCES,
Context.MODE_PRIVATE);
}
public SimpleGeofence getGeofence(String id) {
double lat = mPrefs.getFloat(
getGeofenceFieldKey(id, KEY_LATITUDE),
INVALID_FLOAT_VALUE);
double lng = mPrefs.getFloat(
getGeofenceFieldKey(id, KEY_LONGITUDE),
INVALID_FLOAT_VALUE);
float radius = mPrefs.getFloat(
getGeofenceFieldKey(id, KEY_RADIUS),
INVALID_FLOAT_VALUE);
long expirationDuration = mPrefs.getLong(
getGeofenceFieldKey(id, KEY_EXPIRATION_DURATION),
INVALID_LONG_VALUE);
int transitionType = mPrefs.getInt(
getGeofenceFieldKey(id, KEY_TRANSITION_TYPE),
INVALID_INT_VALUE);
// If none of the values is incorrect, return the object
if (
lat != GeofenceUtils.INVALID_FLOAT_VALUE &&
lng != GeofenceUtils.INVALID_FLOAT_VALUE &&
radius != GeofenceUtils.INVALID_FLOAT_VALUE &&
expirationDuration !=
GeofenceUtils.INVALID_LONG_VALUE &&
transitionType != GeofenceUtils.INVALID_INT_VALUE) {
// Return a true Geofence object
return new SimpleGeofence(
id, lat, lng, radius, expirationDuration,
transitionType);
// Otherwise, return null.
} else {
return null;
}
}
public void setGeofence(String id, SimpleGeofence geofence) {
Editor editor = mPrefs.edit();
// Write the Geofence values to SharedPreferences
editor.putFloat(
getGeofenceFieldKey(id, KEY_LATITUDE),
(float) geofence.getLatitude());
editor.putFloat(
getGeofenceFieldKey(id, KEY_LONGITUDE),
(float) geofence.getLongitude());
editor.putFloat(
getGeofenceFieldKey(id, KEY_RADIUS),
geofence.getRadius());
editor.putLong(
getGeofenceFieldKey(id, KEY_EXPIRATION_DURATION),
geofence.getExpirationDuration());
editor.putInt(
getGeofenceFieldKey(id, KEY_TRANSITION_TYPE),
geofence.getTransitionType());
// Commit the changes
editor.commit();
}
public void clearGeofence(String id) {
Editor editor = mPrefs.edit();
editor.remove(getGeofenceFieldKey(id, KEY_LATITUDE));
editor.remove(getGeofenceFieldKey(id, KEY_LONGITUDE));
editor.remove(getGeofenceFieldKey(id, KEY_RADIUS));
editor.remove(getGeofenceFieldKey(id,
KEY_EXPIRATION_DURATION));
editor.remove(getGeofenceFieldKey(id, KEY_TRANSITION_TYPE));
editor.commit();
}
private String getGeofenceFieldKey(String id,
String fieldName) {
return KEY_PREFIX + "_" + id + "_" + fieldName;
}
}
...
}
|

RESOLU | Obtenir l'emplacement actuel sous AndroidBonjourAvez-vous un exemple qui permet d'obtenir l'emplacement actuel sous Android? |
Android | |
Android |
RESOLU | Spécifier les interfaces et définir les méthodesBonjourAvez-vous une idée comment spécifier les interfaces et définir les méthodes sous Android? |
Android | |
Interfaces Méthodes Android |
RESOLU | Vérifier Google Play servicesBonjourJe voudrais savoir le code qui permet de vérifier Google Play services sous Android? |
Android | |
Google Play Services Android |
RESOLU | Définir un DialogFragment qui affiche la boîte de dialogue d'erreurBonjourComment définir un DialogFragment qui affiche la boîte de dialogue d'erreur sous Android? |
Android | |
DialogFragment Android |
RESOLU | Ajouter la vue de QuickContactBadgeSalutJe voudrais le code qui permet d'ajouter la vue de QuickContactBadge d'Android? |
Android | |
QuickContactBadge Android |
RESOLU | Ajouter le drapeau de navigationBonjourSavez-vous comment ajouter le drapeau de navigation sous Android? |
Android | |
Navigation Android |
RESOLU | Définir l'ordre de tri sous AndroidBonjourJe voudrais savoir le code qui permet de définir l'ordre de tri sous Android? |
Android | |
Android |
RESOLU | Initialiser le chargeur sous AndroidSalutS'il vous plaît avez-vous une idée comment initialiser le chargeur sous Android? |
Android | |
Chargeur Android |
RESOLU | Définir la méthode onItemClick()BonjourJ'ai besoin de savoir comment définir la méthode onItemClick() d'Android? |
Android | |
Méthode Android |
RESOLU | Spécifier les critères de sélectionBonjourJe voudrais savoir s'il vous plaît comment spécifier les critères de sélection sous Android? |
Android | |
Android |
RESOLU | Régler l'auditeur de contact sélectionnéSalutComment régler l'auditeur de contact sélectionné sous Android? |
Android | |
Auditeur Android |
RESOLU | Mettre en place la CursorAdapter pour le ListViewBonjourSavez_vous comment mettre en place la CursorAdapter pour le ListView d'Android? |
Android | |
CursorAdapter ListView Android |
RESOLU | Initialiser le Fragment sous AndroidBonjourJ'aimerais savoir comment puis-je initialise un Fragment sous Android? |
Android | |
Fragment Android |
RESOLU | Définir un fragment sous AndroidBonjourJ'ai besoin de savoir comment définir un fragment sous Android? |
Android | |
Fragment Android |
RESOLU | Comment définir une ListView ?BonjourS'il vous plaît j'ai besoin de savoir comment définir une ListView sous Android? |
Android | |
ListView Android |
RESOLU | Exécuter l'adaptateur de synchronisation à la demandeBonjourSavez-vous comment exécuter l'adaptateur de synchronisation à la demande sous Android? |
Android | |
Adaptateur Synchronisation Android |
RESOLU | Exécuter l'adaptateur de synchronisation après un message réseauBonjourComment exécuter l'adaptateur de synchronisation après un message réseau sous Android? |
Android | |
Adaptateur Synchronisation Réseau Android |
RESOLU | Déclarer l'adaptateur de synchronisation dans le manifesteBonjourJe voudrais savoir comment déclarer l'adaptateur de synchronisation dans le d'Android? |
Android | |
Adaptateur Synchronisation Manifeste Android |
| Veuillez vous connecter pour profiter pleinement de toutes les fonctionnalités de Oyant. | ||
Connexion |
Inscription |
|
|
Poster une question |
||