Я пытаюсь реализовать механизм геозоны, при котором геозона отслеживается, и как только пользователь выходит из текущей геозоны, текущие координаты используются для создания новой геозоны, и инициируется запрос БД для получения некоторых данных.
Моя проблема в том, что ожидающее намерение никогда не срабатывает.
Из журналов я вижу, что геозоны добавляются в клиент местоположения. Однако никакие ожидающие намерения не запускаются при изменении местоположения (я установил радиус забора на 2 м и прошел более 100 м). Что-то не так в том, как я объявил службу намерений?
Вот класс обслуживания намерений.
public class GeoFenceIntentService extends IntentService{
private static final String mIntentName = "GeoFenceIntentService";
public GeoFenceIntentService() {
super(mIntentName);
}
@Override
protected void onHandleIntent(Intent intent) {
int transitionType = LocationClient.getGeofenceTransition(intent);
Log.e(TAG,"Inside fence handler");
if(transitionType == Geofence.GEOFENCE_TRANSITION_EXIT){
//Query DB here with current co-ords
//create new GeoFence
Location location = LocationHelper.getInstance(mContext).getLastLocation();
mLat = String.valueOf(location.getLatitude());
mLong = String.valueOf(location.getLongitude());
addGeofenceToMonitor(location);
queryDb();
}
}
}
Также здесь я добавляю ожидающие намерения и геозону в клиент местоположения.
addGeofenceToMonitor(Location location){
List<Geofence> list = new ArrayList<Geofence>();
list.add(getNewGeofence(location.getLatitude(), location.getLongitude()));
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0,
new Intent(mContext,GeoFenceIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
OnRemoveGeofencesResultListener removeListener = new OnRemoveGeofencesResultListener() {
@Override
public void onRemoveGeofencesByRequestIdsResult(int statusCode, String[] requestIDs) {
//To be used
}
@Override
public void onRemoveGeofencesByPendingIntentResult(int statusCode,PendingIntent pendingIntent) {
//Not used
}
};
LocationHelper.getInstance(mContext).removeGeoFence(mGeofenceRequestIDs, removeListener);
OnAddGeofencesResultListener addListener = new OnAddGeofencesResultListener() {
@Override
public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
if(statusCode != LocationStatusCodes.SUCCESS){
//handle error cases
}
else
Log.i(TAG, "Successfully added Geofence "+geofenceRequestIds[0]+" for monitoring");
}
};
LocationHelper.getInstance(mContext).addGeoFence(list, pendingIntent, addListener);
}
Вот фрагмент из файла манифеста
<service
android:name="com.myexample.sample.GeoFenceIntentService"
android:label="@string/app_name"
android:exported="true">
</service>
BroadcastReceiver
в качестве цели вашегоPendingIntent
вместоService
? Если нет, попробуйте это и посмотрите, вызывается лиonReceive()
. - person David Wasser   schedule 27.01.2014