android - 带有 BroadcastReceiver 的地理围栏并不总是被触发

标签 android android-geofence

之前,当我在我的应用程序中使用地理围栏时,我使用 IntentService 作为其回调,一切正常。但现在由于 Android 8 的变化,我无法再启动这样的服务了。因此,现在我使用 BroadcastReceiver 作为我的 Geofencing 回调,并且该接收器正在启动服务(android 8 的前景之一)。但随着这一变化,我注意到地理围栏不再有效。它通常根本不会被触发,我认为最糟糕的情况是应用程序在前台。最好的情况是当应用程序被终止时,地理围栏就会被触发。 BroadcastReceivers 和地理围栏有问题还是我做错了什么?我提供我的代码:

用于添加地理围栏的通用类:

public abstract class BaseGeofence<T extends BaseGeofenceObject> implements OnSuccessListener<Void>, OnFailureListener {
    protected int RADIUS_IN_METERS = 300;
    protected GeofencingClient client;
    protected Context context;

    public BaseGeofence(Context context) {
        this.client = LocationServices.getGeofencingClient(context);
        this.context = context;
    }

    @SuppressLint("MissingPermission")
    public void addGeofencing(final T object) {
        if (GPSUtils.wasLocationChecked()) {
            Geofence geofence = (new Geofence.Builder()
                    .setRequestId(Integer.toString(getId(object)))

                    .setCircularRegion(
                            object.getLat(),
                            object.getLon(),
                            RADIUS_IN_METERS
                    )
                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                            Geofence.GEOFENCE_TRANSITION_EXIT)
                    .setExpirationDuration(Geofence.NEVER_EXPIRE)
                    .build());
            client.addGeofences(getGeofencingRequest(geofence), getGeofencePendingIntent(object))
                    .addOnSuccessListener(this).addOnFailureListener(this);
        }


    }

    private GeofencingRequest getGeofencingRequest(Geofence geofence) {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofence(geofence);
        return builder.build();
    }

    private PendingIntent getGeofencePendingIntent(T object) {
        return PendingIntent.getBroadcast(context, getId(object), createIntent(object), PendingIntent.FLAG_UPDATE_CURRENT);
    }

    protected abstract Intent createIntent(T object);

    protected abstract int getId(T object);

    @Override
    public abstract void onSuccess(Void aVoid);

    @Override
    public abstract void onFailure(@NonNull Exception e);
}

MyGeofencing 类:

public class MyGeofencing extends BaseGeofence<MyObject> {

        private MyObject myObject;

        public MyGeofencing(Context context) {
            super(context);
        }

        @Override
        protected Intent createIntent(MyObject object) {
            this.myObject = object;
            Intent intent = new Intent(context, GeofenceTransitionsReceiver.class);

            //put extras to intent 

            return intent;
        }

        @Override
        public void addGeofencing(MyObject object) {
            if (object.getGeofencingRadius() == 0) RADIUS_IN_METERS = 300;
            else RADIUS_IN_METERS = object.getGeofencingRadius();
            super.addGeofencing(object);
        }

        @Override
        protected int getId(MyObject object) {
            return object.getId();
        }



    }

我的接收器:

public class GeofenceTransitionsReceiver extends StartingServiceBroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        if (GeofencingEvent.fromIntent(intent) != null && GeofencingEvent.fromIntent(intent).getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER) {

            startConnectionService(context, "extra");


        } else if (GeofencingEvent.fromIntent(intent) != null && GeofencingEvent.fromIntent(intent).getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) {
            // do something
        }
    }

protected void startConnectionService(Context context, String extra) {
        Intent beaconIntent = new Intent(context, BeaconConnectionService.class);
        beaconIntent.putExtra("extra", extra);        
        startService(context, beaconIntent);
    }
}

StartingServiceBroadcastReceiver 启动前台服务,如果是 android 8 否则是正常的

public abstract class StartingServiceBroadcastReceiver extends BroadcastReceiver {
    @Override
    public abstract void onReceive(Context context, Intent intent);

    protected void startService(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else context.startService(intent);
    }
}

我的服务扩展了 FlexibleFOregroundService,它在需要时启动前台通知:

public class FlexibleForegroundService extends Service {

    private static final String CHANNEL_ID = "test";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        makeServiceForegroundIfNeeded();
    }

    public void stopService(){
        stopSelf();
    }

    private void makeServiceForegroundIfNeeded() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();

            String message = "test";

            Notification notification =
                    new Notification.Builder(this, CHANNEL_ID)
                            .setContentTitle("test")
                            .setStyle(new Notification.BigTextStyle()
                                    .bigText(message))
                            .setContentText(message)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .build();

            startForeground(1, notification);
        }
    }


    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Information", importance);
            channel.setDescription("test");
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }




}

有什么方法可以改进地理围栏在所有 Android 设备上的工作吗?

最佳答案

我假设您已经知道在哪些情况下您必须 Reregister geofences 只要用户禁用 GPS,您的地理围栏就会被注销。

我观察到的是,当您使用位置服务(例如 map 打开时)时,您会更频繁地收到地理围栏事件。

您是否也可以尝试将“ACTION_GEOFENCE_TRANSITION”之类的操作放入 list 文件中的接收器 Intent 过滤器中。在创建待定 Intent 时将其作为操作放置。

关于android - 带有 BroadcastReceiver 的地理围栏并不总是被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52928561/

相关文章:

android - 设置刷新 Activity 图

android - 如何在 ddms logcat 中禁用不需要的设备日志

android - 使用 holoeverywhere 库时,我的应用程序在 setcontentview 上强制关闭

android - 如何在Geofence android中添加后台服务

android - IntentService 不会与 PendingIntent 一起调用

应用程序被杀死时的 Android 地理围栏

java - 升级 Android Studio NDK : CMAKE_C_COMPILER and CMAKE_CXX_COMPILER not set 后出错

android - 在像gmail这样的android中显示复杂的Html文本

android - Android Q 中引入的 ACCESS_BACKGROUND_LOCATION 如何影响 Geofence API?

android - 来自 LocationServices 的 getTriggeringGeofences 和 getGeofenceTransition