我正在开发一个使用新的 android 地理围栏 API 的 android 应用程序,我想注册 100 多个地理围栏。
我每 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 的所有内容都在后台完美运行