在PendingIntent中进行位置更新后,在android中删除和添加地理围栏

时间:2013-09-13 11:02:44

标签: android android-pendingintent android-geofence

我正在开发一个使用新的android geofencing API的Android应用程序,我想注册超过100个Geofences。
我每隔1分钟在后台更新一次我的位置,而IntentService类中的onHandleIntent方法我正在计算当前位置与获得最接近的100之间的距离,然后我需要监控这100个地理围栏。

问题是我在获取当前位置后无法添加最接近的100个地理围栏,所有这些都必须在应用程序关闭时起作用。

这是我的代码:

package com.example.android.geofence;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.Toast;

import com.example.android.geofence.GeofenceUtils.REMOVE_TYPE;
import com.example.android.geofence.GeofenceUtils.REQUEST_TYPE;
import com.example.android.geofence.MainActivity.GeofenceSampleReceiver;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener;

public class LocationService extends IntentService implements OnAddGeofencesResultListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

private String TAG = this.getClass().getSimpleName();
public static int notificationID = 0;

LocationClient geoLocationClient;

public String monitoredLounges = "";

public LocationService() {
    super("Fused Location");
}

public LocationService(String name) {
    super("Fused Location");
}

@Override
   public void onCreate() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resultCode == ConnectionResult.SUCCESS) {
        geoLocationClient = new LocationClient(this, this, this);
        // when I'm using connect() in order to addgeofences my application stops unexpectedly
        geoLocationClient.connect();
    }
}

@Override
protected void onHandleIntent(Intent intent) {
    Location location = intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);

    if(location != null){
        RegisterGeofences(location);

        Log.i(TAG, "onHandleIntent " + location.getLatitude() + "," + location.getLongitude());
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
        Builder noti = new NotificationCompat.Builder(this);
        noti.setContentTitle("Fused Location");
        noti.setContentText(notificationID + "," + location.getLatitude() + "," + location.getLongitude());
        noti.setSmallIcon(R.drawable.ic_launcher);
        notificationManager.notify(notificationID, noti.build());
        notificationID++;
    }
}

public void RegisterGeofences(Location _location) {
    MySQLiteHelper myDbHelper = new MySQLiteHelper(this);
    List<Lounge> closestRegions = myDbHelper.getClosestRegions(_location.getLatitude(), _location.getLongitude());
    ArrayList<Geofence> geofenceList = new ArrayList<Geofence>();

    int regionIndex = 0;
    for (Region cn : closestRegions){
        regionIndex++;
        if(regionIndex> 100) break;

            Geofence geofence = new Geofence.Builder()
                .setRequestId(cn.getTitle())
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .setCircularRegion(cn.getLatitude(), cn.getLongitude(), (float) 500.00)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .build();
            monitoredLounges += cn.getTitle() + "&";
            geofenceList.add(geofence);

    }
    PendingIntent geoFencePendingIntent = PendingIntent.getService(this, 0,
            new Intent(this, GeofenceIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);

    geoLocationClient.addGeofences(geofenceList, geoFencePendingIntent, this);      

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

@Override
public void onAddGeofencesResult(int arg0, String[] arg1) {
    // TODO Auto-generated method stub

}

}

另请注意,在添加RegisterGeofences方法之前,有关updatelocations的所有内容都在后台完美运行

1 个答案:

答案 0 :(得分:0)

在作业完成后

IntentService不运行。(即)它会在一段时间后销毁服务。相反,您创建一个service并运行它们。因为GeoFences仅在geoLocationClient连接且可用时运行。同时断开服务geoLocationClient

中的onDestroy()