gpt4 book ai didi

Android LocationServices.GeofencingApi 示例用法

转载 作者:可可西里 更新时间:2023-11-01 18:56:32 27 4
gpt4 key购买 nike

有人知道使用 LocationServices.GeofencingApi 的示例吗?我发现的所有 android 地理围栏示例都使用已弃用的 LocationClient 类。据我所知,可以使用 LocationServices 类,但似乎没有关于如何使用它的任何工作示例。

我找到的最接近的是 this发布突出显示位置更新请求

更新:我找到的最接近的答案是 this git example项目 - 但它仍然使用已弃用的 LocationClient 来触发栅栏。

最佳答案

我刚刚将我的代码迁移到新的 API。这是一个工作示例:

GitHub 上基于此答案的工作项目:https://github.com/androidfu/GeofenceExample

This helper class registers the geofences using the API. I use a callback interface to communicate with the calling activity/fragment. You can build a callback that fits your needs.

public class GeofencingRegisterer implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private List<Geofence> geofencesToAdd;
private PendingIntent mGeofencePendingIntent;

private GeofencingRegistererCallbacks mCallback;

public final String TAG = this.getClass().getName();

public GeofencingRegisterer(Context context){
mContext =context;
}

public void setGeofencingCallback(GeofencingRegistererCallbacks callback){
mCallback = callback;
}

public void registerGeofences(List<Geofence> geofences){
geofencesToAdd = geofences;

mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}


@Override
public void onConnected(Bundle bundle) {
if(mCallback != null){
mCallback.onApiClientConnected();
}

mGeofencePendingIntent = createRequestPendingIntent();
PendingResult<Status> result = LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, geofencesToAdd, mGeofencePendingIntent);
result.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
// Successfully registered
if(mCallback != null){
mCallback.onGeofencesRegisteredSuccessful();
}
} else if (status.hasResolution()) {
// Google provides a way to fix the issue
/*
status.startResolutionForResult(
mContext, // your current activity used to receive the result
RESULT_CODE); // the result code you'll look for in your
// onActivityResult method to retry registering
*/
} else {
// No recovery. Weep softly or inform the user.
Log.e(TAG, "Registering failed: " + status.getStatusMessage());
}
}
});
}

@Override
public void onConnectionSuspended(int i) {
if(mCallback != null){
mCallback.onApiClientSuspended();
}

Log.e(TAG, "onConnectionSuspended: " + i);
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if(mCallback != null){
mCallback.onApiClientConnectionFailed(connectionResult);
}

Log.e(TAG, "onConnectionFailed: " + connectionResult.getErrorCode());
}



/**
* Returns the current PendingIntent to the caller.
*
* @return The PendingIntent used to create the current set of geofences
*/
public PendingIntent getRequestPendingIntent() {
return createRequestPendingIntent();
}

/**
* Get a PendingIntent to send with the request to add Geofences. Location
* Services issues the Intent inside this PendingIntent whenever a geofence
* transition occurs for the current list of geofences.
*
* @return A PendingIntent for the IntentService that handles geofence
* transitions.
*/
private PendingIntent createRequestPendingIntent() {
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
} else {
Intent intent = new Intent(mContext, GeofencingReceiver.class);
return PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
}

This class is the base class for your geofence transition receiver.

public abstract class ReceiveGeofenceTransitionIntentService extends IntentService {

/**
* Sets an identifier for this class' background thread
*/
public ReceiveGeofenceTransitionIntentService() {
super("ReceiveGeofenceTransitionIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {

GeofencingEvent event = GeofencingEvent.fromIntent(intent);
if(event != null){

if(event.hasError()){
onError(event.getErrorCode());
} else {
int transition = event.getGeofenceTransition();
if(transition == Geofence.GEOFENCE_TRANSITION_ENTER || transition == Geofence.GEOFENCE_TRANSITION_DWELL || transition == Geofence.GEOFENCE_TRANSITION_EXIT){
String[] geofenceIds = new String[event.getTriggeringGeofences().size()];
for (int index = 0; index < event.getTriggeringGeofences().size(); index++) {
geofenceIds[index] = event.getTriggeringGeofences().get(index).getRequestId();
}

if (transition == Geofence.GEOFENCE_TRANSITION_ENTER || transition == Geofence.GEOFENCE_TRANSITION_DWELL) {
onEnteredGeofences(geofenceIds);
} else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
onExitedGeofences(geofenceIds);
}
}
}

}
}

protected abstract void onEnteredGeofences(String[] geofenceIds);

protected abstract void onExitedGeofences(String[] geofenceIds);

protected abstract void onError(int errorCode);
}

This class implements the abstract class and does all the handling of geofence transitions

public class GeofencingReceiver extends ReceiveGeofenceTransitionIntentService {

@Override
protected void onEnteredGeofences(String[] geofenceIds) {
Log.d(GeofencingReceiver.class.getName(), "onEnter");
}

@Override
protected void onExitedGeofences(String[] geofenceIds) {
Log.d(GeofencingReceiver.class.getName(), "onExit");
}

@Override
protected void onError(int errorCode) {
Log.e(GeofencingReceiver.class.getName(), "Error: " + i);
}
}

And in your manifest add:

<service
android:name="**xxxxxxx**.GeofencingReceiver"
android:exported="true"
android:label="@string/app_name" >
</service>

Callback Interface

public interface GeofencingRegistererCallbacks {
public void onApiClientConnected();
public void onApiClientSuspended();
public void onApiClientConnectionFailed(ConnectionResult connectionResult);

public void onGeofencesRegisteredSuccessful();
}

关于Android LocationServices.GeofencingApi 示例用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26633796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com