我正在为我的应用使用Geofencing,我按照声明here的步骤 但每当我尝试初始化GoogleApiClient时,我都无法收到任何回电信息。以下是我的代码..
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks)this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
我得到一个强制关闭错误
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
onCreate()中的代码
geoFence = new Geofence.Builder()
.setRequestId("DVSN")
.setCircularRegion(18.5553785, 73.8164671, 10)
.setExpirationDuration(43200000)
.setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_ENTER
| Geofence.GEOFENCE_TRANSITION_EXIT).build();
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(this);
私人方法
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofence(geoFence);
return builder.build();
}
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent
// back when
// calling addGeofences() and removeGeofences().
return PendingIntent.getService(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
答案 0 :(得分:0)
首先连接您的GoogleApiClient
,然后将地理信息添加到GeofencingApi
。
mApiClient = new GoogleApiClient.Builder(mContext)
.addApi(LocationServices.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected to GoogleApiClient");
addGeofencesOnLoad();
}
@Override
public void onConnectionSuspended(int i) {
}
}).build();
mApiClient.connect();
public void addGeofencesOnLoad() {
if (!mGoogleApiClient.isConnected()) {
Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
return;
}
try {
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(this); // Result processed in onResult().
} catch (SecurityException securityException) {
logSecurityException(securityException);
}
}
首先,使用Geofence.Builder
创建地理围栏,为地理围栏设置所需的半径,持续时间和过渡类型。例如,要填充名为mGeofenceList
mGeofenceList.add(new Geofence.Builder()
// Set the request ID of the geofence. This is a string to identify this
// geofence.
.setRequestId(entry.getKey())
.setCircularRegion(
entry.getValue().latitude,
entry.getValue().longitude,
Constants.GEOFENCE_RADIUS_IN_METERS
)
.setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
以下代码段使用GeofencingRequest
类及其嵌套的GeofencingRequestBuilder
类来指定要监控的地理围栏以及设置触发相关地理围栏事件的方式
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
}
以下代码段显示如何定义启动PendingIntent
的{{1}}
IntentService