2019年如何处理背景围栏?

时间:2019-04-28 17:13:28

标签: java android geofencing android-geofence

这是情况:

当用户创建地理围栏时,我将其保存到后端,并在操作系统中注册了地理围栏。但是,只要我的应用程序重新启动,我就会从后端获取地理围栏,然后再次在操作系统中重新注册它们,因为它们不断消失。

我有两个班MainActivityFormActivity。这两个活动都注册了地理围栏,因此我已经将实际注册提取到普通的POJO Geofences.java

这是问题所在:

现在很奇怪的是,仅在屏幕上显示地图活动时才接收触发器。我的应用程序中确实有地图活动,但是即使我启动了谷歌地图地理围栏触发器也开始触发,它甚至不必是我的地图活动。

我在做什么错了?

Geofences.java:

public class Geofences {

    private final String TAG = Geofences.class.getSimpleName();
    private final float RADIUS = 150.0F; //meter
    private boolean success = false;

    private final int LOITERING_IN_MILLISECONDS = 30000;// 30 seconds

    public boolean doGeofenceStuff(GeoTemp newTemp, String geofenceId, PendingIntent pendingIntent, GeofencingClient geofencingClient) {

        Geofence geofence = createGeofence(newTemp, geofenceId);
        GeofencingRequest geofencingRequest = createGeofenceRequest(geofence);
        geofencingClient.addGeofences(geofencingRequest, pendingIntent)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            success = true;
                            Log.i(TAG, "onComplete: DEBUG-Message: Geofence has been added.");

                        } else {
                            success = false;
                            Log.i(TAG, "onComplete: Geofence could not be added");
                        }
                    }
                }); // handle error here
        return success;
    }

    // Create a Geofence
    private Geofence createGeofence(GeoTemp geoTemp, String geofenceId) {


        long expiration = getExpirationForCurrentGeofence();
        if (expiration < 1) {
            Log.e(TAG, "createGeofence: Can't create Geofence, since expiration is less than zero");
            return null;
        }
        Log.d(TAG, "createGeofence");
        return new Geofence.Builder()
                .setRequestId(geofenceId)
                .setCircularRegion(getLat(), getLong(), RADIUS)
                .setExpirationDuration(expiration)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_DWELL | Geofence.GEOFENCE_TRANSITION_EXIT)
                .setLoiteringDelay(LOITERING_IN_MILLISECONDS)
                .build();
    }

    // Create a Geofence Request
    private GeofencingRequest createGeofenceRequest(Geofence geofence) {
        Log.d(TAG, "createGeofenceRequest");
        return new GeofencingRequest.Builder()
                .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL)
                .addGeofence(geofence)
                .build();
    }

}

此POJO Geofences.java随后被我的两项活动使用:

MainActivity:

public class MainActivity extends AppCompatActivity {

    private static String TAG = "MainActivity";
    private final int GEOFENCE_REQ_CODE = 0;
    private GeofencingClient geofencingClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        geofencingClient = LocationServices.getGeofencingClient(this);
        getCurrentTemps();
    }

    private void refreshGeofence(GeoTemp temp, String id) {
        new Geofences().doGeofenceStuff(temp, id, createGeofencePendingIntent(), geofencingClient);
    }
    private void getCurrentTemps() {
        List<GeoTemp> currentGeofences = getUpdatedList();
        currentGeofences.forEach(geoTemp -> {
            refreshGeofence( geoTemp, id);
        });
    }
    private PendingIntent createGeofencePendingIntent() {
        Log.d(TAG, "createGeofencePendingIntent");
        Intent intent = new Intent(this, LocationAlertIntentService.class);
        return PendingIntent.getService(
                this, GEOFENCE_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
}

还有另外一个活动,它使用Geofences.java在操作系统中注册geofence。

更新:

我发现,如果有其他任何应用程序(包括我的)请求位置锁定,则地理围栏会触发火灾。我需要它们在后台开火。

1 个答案:

答案 0 :(得分:1)

在android中使用地理围栏时,我遇到了类似的问题。

发生这种情况是由于在Android Oreo及更高版本中添加了background restrictions

OS不允许您的应用在后台运行时启动服务,因此您不会收到地理围栏触发。

要处理此问题:

  • 添加广播接收器以接收意图。 (此接收器将 地理围栏警报,即使应用程序在后台)
  • 用JobIntentService替换服务。 (这将使用操作系统JobSchedular 甚至可以在有背景限制的情况下运行)
  • 从待定意图而非服务中获取广播。

查看此sample project以获得进一步的说明。