【问题标题】:Do background geofences really work on Android 8+?背景地理围栏真的适用于 Android 8+ 吗?
【发布时间】:2023-03-14 13:41:01
【问题描述】:

我有一个地理围栏应用程序,我正在尝试移植它以在 Android 8+ 上运行。我已经阅读了有关该主题的tutorial,并切换到使用compile 'com.google.android.gms:play-services-location:16.0.0'

当应用不在前台时,地理围栏进入事件永远不会触发。我等待的时间超过了文档所说的两分钟。我等了15分钟没有结果。只要我将应用程序带到地理围栏内的前台,它就会立即触发。

我知道在 Android 8+ 上对后台服务有限制,但 Google 的教程说要使用应该在 Android 8+ 上被阻止的 IntentService。 后见之明编辑:上述教程是绝对错误的。不要跟随它。 IntentService 将不起作用。

我尝试关注this answer,它说使用新的GeofencingClient 来设置您的意图来解决这个问题。 (我不明白为什么这会有所帮助,因为它仍然使用 IntentService 来接收事件,并且它在我的代码中不起作用。)

这个对相关问题here 的回答建议您必须使用BroadcastReceiver 而不是IntentService,但我不明白为什么这会有所不同,因为Android 8 在后台对BroadcastReceiver 施加了与IntentServices 相同的限制. 后见之明编辑:这个链接的答案也是正确的。虽然隐式广播接收器受到限制,但在运行时注册以向特定包发送请求的显式广播接收器不受限制并且可以正常工作。

真的有可能在 Android 8+ 上获得在后台唤醒您的应用的地理围栏回调吗?如果是这样,你怎么能做到这一点?

我的地理围栏设置:

googleApiClient = new GoogleApiClient.Builder(this.context)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(getServiceSetup())
        .addOnConnectionFailedListener(getServiceFailureStrategy())
        .build();
geofencingClient = LocationServices.getGeofencingClient(context);

Intent intent =  new Intent(context, mIntentService);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
// calling addGeofences() and removeGeofences().
geofencePendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.
            FLAG_UPDATE_CURRENT);

geofencingClient.addGeofences(geofences, geofencePendingIntent)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                 @Override
                   public void onSuccess(Void aVoid) {
                     getAddGeofencesCallback(runnable, geofencesToAdd).onResult(new Status(0));
                     Log.e(TAG, "Successfully added geofences with GeofencingClient");
                   }
                 })
                .addOnFailureListener(new OnFailureListener() {
                   @Override
                   public void onFailure(Exception e) {
                     getAddGeofencesCallback(runnable, geofencesToAdd).onResult(new Status(1));
                     Log.e(TAG, "Failed to add geofences with GeofencingClient", e);

意图服务:

public class GeofenceService extends IntentService {
    private static final String TAG = "GeofenceService";

    public GeofenceService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        GeofenceTransition transition = GeofenceServiceManager.getGeofenceTransition(intent);
        if (transition == null) {
            return;
        }

        GeofenceServiceManager.logd(
                TAG,
                "onHandleIntent. geofence: " +
                        GeofenceMessage.notificationTitleFor(this, transition)
        );
        processErrors(transition);
        sendBroadcast(transition);
    }

    private Intent generateBroadcast(GeofenceTransition transition) {
        Intent broadcastIntent = new Intent();

        broadcastIntent.addCategory(GeofenceUtils.CATEGORY_GEOFENCE_SERVICES)
                       .setAction(GeofenceUtils.actionNameFor(transition))
                       .putStringArrayListExtra(
                               GeofenceUtils.GEOFENCE_IDS,
                               new ArrayList<String>(transition.getIds())
                                               );

        return broadcastIntent;
    }

    private void sendBroadcast(GeofenceTransition transition) {
        if (transition.isError() || transition.unknownType()) {
            GeofenceServiceManager.logd(TAG, "geofence transition is error or unknown type.");
            return;
        }

        broadcastManager().sendBroadcast(generateBroadcast(transition));
    }

    private LocalBroadcastManager broadcastManager() {
        return LocalBroadcastManager.getInstance(this);
    }

    private void processErrors(GeofenceTransition transition) {
        if (!transition.isError()) {
            return;
        }

        Log.e(TAG, "geofence error: "+GeofenceMessage.errorDetailsFor(this, transition));

        Intent broadcastIntent = generateBroadcast(transition);
        broadcastIntent.putExtra(
                GeofenceUtils.GEOFENCE_STATUS,
                GeofenceMessage.errorMessageFor(this, transition)
                                );
        broadcastManager().sendBroadcast(broadcastIntent);
    }
}

AndroidManifest.xml:

...
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

      <service
          android:name=".geofence.GeofenceService"
          android:enabled="true"
          android:exported="false">
      </service>
...

编辑 1:根据 Google 记录在案的 Android 8+ 后台处理限制,在我看来这是不可能的。 Android 8+ 在进入后台 10 分钟内杀死正在运行的应用程序,并在不在前台时阻止启动服务,包括 Intent 服务。然而谷歌说:

注意:在 Android 8.0(API 级别 26)及更高版本中,如果应用在监控地理围栏时在后台运行,则设备每隔几分钟就会响应一次地理围栏事件。要了解如何使您的应用适应这些响应限制,请参阅后台位置限制。

鉴于记录在案的后台执行限制,这怎么可能?

编辑 2: 我看到清单中声明的​​ BroadcastReceiver 使用意图交付的蓝牙 LE 检测将应用程序启动到 Android 8+ 的后台。该代码如下所示:

Intent intent = new Intent();
intent.setComponent(new ComponentName(context.getPackageName(), "com.example.MyBroadcastReceiver"));
<receiver android:name="com.example.MyBroadcastReceiver">
         </receiver>

这确实适用于 BLE 检测以将应用程序启动到后台。如果传递给 BroadcastReceiver,像上面这样的系统发起的意图是否会以某种方式免受 Android 8+ 后台执行限制(以 IntentServices 没有的方式)?如果是这样,这是否适用于地理围栏? (剧透警告:是的!请阅读下面接受的答案。)

【问题讨论】:

    标签: android location android-8.0-oreo geofencing android-geofence


    【解决方案1】:

    你需要更换:

    geofencePendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.
                FLAG_UPDATE_CURRENT); 
    

    Intent intent = new Intent(this, GeofenceBroadcastReceiver.class); 
    mGeofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    为了支持 Android 8,您应该使用 BroadcastReceiver 来接收地理围栏意图,因为Background Execution Limits(服务不能在后台运行)

    查看详情:https://github.com/android/location-samples/commit/5f83047c8a462d7c619f6275b624e219b4622322

    google 示例如何使用地理围栏:https://github.com/googlesamples/android-play-location/tree/master/Geofencing

    【讨论】:

    • 感谢您的建议。我正在按照您的建议测试切换到广播接收器。我看到您引用的示例确实做到了这一点。不幸的是,developer.google.com still says to use a PendingIntent to start an IntentService 上的地理围栏培训页面。
    • 这确实有效。底线是谷歌的培训页面告诉你使用 PendingIntent 到上面链接的 IntentService 是完全错误的。明确的 BroadcastReceiver 注册确实可以在后台启动应用程序。
    【解决方案2】:

    如果您相信自己在this 文章中所做的一切都是正确的,您可以使用ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 设置。此设置禁用应用程序的电池优化。所以意图不会被系统阻止。因为有时系统会阻止我们的工作以延长电池寿命。因此,如果您的应用程序使用此设置运行,您可以责怪系统。

    还有一件事。您是如何测试地理围栏的?

    【讨论】:

    • 感谢您提供有关忽略电池优化的提示。我当然希望这不是必需的!我通过在距离我的 100m 地理围栏超过 1 公里时将应用程序置于前台进行测试,然后通过点击主页按钮将其置于后台,等待 1 多个小时,然后前往地理围栏的中心并等待长达 15 分钟即使我点亮屏幕,我的本地通知也不会触发。当我解锁手机并将应用程序置于前台时,它会立即启动。
    • 您可以使用 Lockito 等虚假定位应用进行测试。那比真实位置更稳定。使用真实位置可能会得到错误的结果。因为地理围栏触发在实际场景中并不稳定。您可以确定假位置。
    • 我试过 Lockito,它在装有 Android 9 的诺基亚 6.1+ 上重现了同样的问题。我根本不会在装有 Android 7 的华为 P9 上运行。
    猜你喜欢
    • 2021-12-22
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 2013-10-26
    相关资源
    最近更新 更多