Android Vitals问题只有1个地理围栏热点

时间:2018-11-23 05:48:36

标签: android android-studio android-geofence android-vitals

几天前,在我们的应用程序中,Android生命体面临很多问题,因此我们关闭了所有地理围栏热点,生命体还可以,但是现在我们仅打开了一个半径为15 km的热点,并为ENTER,EXIT启用了回调&DWELL,但我们又面临着只有1个热点的重大问题。.不知道这里发生了什么,如果有人有任何想法请帮助..以下是代码-

public class GeoFenceManager implements OnCompleteListener<Void> {

/**
 * Provides access to the Geofencing API.
 */
private GeofencingClient mGeofencingClient;

/**
 * The list of geofences used in this sample.
 */
private ArrayList<Geofence> mGeofenceList;

/**
 * Used when requesting to add or remove geofences.
 */
private PendingIntent mGeofencePendingIntent;

private WeakReference<Context> context;

private static GeoFenceManager geoFenceManager;

public static synchronized GeoFenceManager getInstance(Context context) {
    if (geoFenceManager == null) {
        geoFenceManager = new GeoFenceManager();
    }
    GeoFenceManager.geoFenceManager.context = new WeakReference<>(context);
    return geoFenceManager;
}

private GeoFenceManager() {

}

public void init(Context context) {
    mGeofencePendingIntent = null;
    // Empty list for storing geofences.
    if (mGeofenceList == null) {
        mGeofenceList = new ArrayList<>();
    } else {
        mGeofenceList.clear();
    }
    mGeofencingClient = LocationServices.getGeofencingClient(context);
    populateGeofenceList();
    addGeofences();
}

@Override
public void onComplete(@NonNull Task<Void> task) {
    if (task.isSuccessful()) {
        Logger.DEBUG("Geofence added");
    } else {
        // Get the status code for the error and log it using a user-friendly message.
        String errorMessage = GeofenceErrorMessages.getErrorString(context.get(), task.getException());
        Logger.DEBUG(errorMessage);
    }
}

/**
 * Adds geofences. This method should be called after the user has granted the location
 * permission.
 */
@SuppressWarnings("MissingPermission")
private void addGeofences() {
    if (!checkPermissions()) {
        return;
    }
    if (mGeofencingClient != null) {
        mGeofencingClient.removeGeofences(getGeofencePendingIntent()).addOnCompleteListener(this);
        if (mGeofenceList != null && mGeofenceList.size() > 0) {
            mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
                    .addOnCompleteListener(this);
        }
    }
}

/**
 * Builds and returns a GeofencingRequest. Specifies the list of geofences to be monitored.
 * Also specifies how the geofence notifications are initially triggered.
 */
private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();

    // The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
    // GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
    // is already inside that geofence.
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL);

    // Add the geofences to be monitored by geofencing service.
    builder.addGeofences(mGeofenceList);

    // Return a GeofencingRequest.
    return builder.build();
}

/**
 * Removes geofences. This method should be called after the user has granted the location
 * permission.
 */
@SuppressWarnings("MissingPermission")
public void removeGeofences() {
    if (!checkPermissions()) {
        return;
    }
    if (mGeofenceList != null) {
        mGeofenceList.clear();
    }
    if (mGeofencingClient != null) {
        mGeofencingClient.removeGeofences(getGeofencePendingIntent()).addOnCompleteListener(this);
    }
}

/**
 * Return the current state of the permissions needed.
 */
private boolean checkPermissions() {
    int permissionState = ActivityCompat.checkSelfPermission(context.get(),
            Manifest.permission.ACCESS_FINE_LOCATION);
    return permissionState == PackageManager.PERMISSION_GRANTED;
}

/**
 * Gets a PendingIntent to send with the request to add or remove 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 getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(context.get(), GeofenceTransitionsIntentService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // addGeofences() and removeGeofences().
    return PendingIntent.getService(context.get(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

/*
 * This sample hard codes geofence data. A real app might dynamically create geofences based on
 * the user's location.
 */
private void populateGeofenceList() {
    mGeofenceList.clear();
    HotSpotResponse hotSpotResponse = PreferenceKeeper.getHotspotData(context.get());
    if (hotSpotResponse != null && hotSpotResponse.hotspots != null && hotSpotResponse.hotspots.size() > 0) {
        for (HotSpots hotSpot : hotSpotResponse.hotspots) {
            if (hotSpot.radius > 0) {
                mGeofenceList.add(new Geofence.Builder()
                        // Set the request ID of the geofence. This is a string to identify this
                        // geofence.
                        .setNotificationResponsiveness(hotSpot.notificationResponsiveTime != null ? hotSpot.notificationResponsiveTime : 420000)
                        .setRequestId(hotSpot.isOuter ? AppConstant.MY_GEOFENCE_ID : hotSpot.id + "")

                        // Set the circular region of this geofence.
                        .setCircularRegion(
                                hotSpot.center.lat,
                                hotSpot.center.lng,
                                hotSpot.radius
                        )

                        // Set the expiration duration of the geofence. This geofence gets automatically
                        // removed after this period of time.
                        .setExpirationDuration(Geofence.NEVER_EXPIRE)

                        .setLoiteringDelay((int) hotSpot.dwellTime) //hard coded the dwell time as 10 minutes for now for each hotspot

                        // Set the transition types of interest. Alerts are only generated for these
                        // transition. We track entry and exit transitions in this sample.
                        .setTransitionTypes(hotSpot.transitionType != 0 ? hotSpot.transitionType : 7)
                        // Create the geofence.
                        .build());
            }
        }
    }
}

}

0 个答案:

没有答案