Android 8+上的后台地理围栏是否真正有效?

6
我有一个地理围栏应用程序,我正在尝试将其移植以在Android 8+上运行。我已经阅读了与此相关的教程,并切换到使用compile 'com.google.android.gms:play-services-location:16.0.0'
当应用程序不在前台运行时,地理围栏进入事件永远不会触发。我等待超过文档所说可能需要的两分钟。我等待了15分钟,没有结果。只要我把应用程序带到地理围栏内的前景,它就立即触发。
我了解到,在Android 8+上有关于后台服务的限制,但是Google的教程说要使用一个应该被阻止的IntentService。事后编辑:上述教程是完全错误的,请不要遵循它。IntentService不会起作用。
我尝试遵循这个答案,该答案说要使用新的GeofencingClient来设置您的意图以解决此问题。(我不明白为什么这会有所帮助,因为它仍然使用IntentService接收事件,并且在我的代码中无法工作。) 这里与相关问题的答案建议您必须使用BroadcastReceiver而不是IntentService,但我不认为这会有任何区别,因为Android 8对后台中的BroadcastReceivers和IntentServices施加了相同的限制。事后编辑:此链接的回答也是正确的。虽然隐式BroadcastReceivers受到限制,但在运行时注册以向特定包发送请求的显式BroadcastReceivers没有限制,并且可以正常工作。
在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);

IntentService:

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: 根据谷歌在 Android 8+ 后台处理方面所记录的极限,我认为这是不可能的。Android 8+ 在应用进入后台后的10分钟内关闭正在运行的应用程序,并阻止启动服务,包括意图服务,并且不处于前台时也是如此。尽管谷歌说:

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

考虑到已记录的后台执行限制,这怎么可能呢?

编辑 2: 我看到在清单中声明的广播接收器可以使用通过传递的 Bluetooth LE 检测向 Android 8+ 中的应用程序启动到后台。代码如下:

Intent intent = new Intent();
intent.setComponent(new ComponentName(context.getPackageName(), "com.example.MyBroadcastReceiver"));

<receiver android:name="com.example.MyBroadcastReceiver">
         </receiver>

这可以用于BLE检测将应用程序启动到后台。如果像上面那样的系统发起的意图被传递到广播接收器中,是否会免受Android 8+后台执行限制的影响(以IntentService方式不受其影响)?如果是这样,那么这是否适用于地理围栏?(剧透:是的!请阅读下面接受的答案。)

2个回答

5

您需要替换:

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)接收地理围栏(geofence)意图,由于后台执行限制 (服务不能在后台运行)

详情请参见:https://github.com/android/location-samples/commit/5f83047c8a462d7c619f6275b624e219b4622322

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


谢谢您的建议。我正在测试切换到BroadcastReceiver,就像您建议的那样。我看到您提到的示例确实这样做了。不幸的是,developer.google.com上的Geofence培训页面仍然建议使用PendingIntent来启动IntentService。 - davidgyoung
这是有效的。底线是,谷歌的培训页面告诉你使用PendingIntent链接到IntentService是完全错误的。显式的BroadcastReceiver注册可以在后台启动应用程序。 - davidgyoung

1
如果您相信自己可以按照this文章的要求做到一切正确,那么您可以使用ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS设置。此设置将禁用应用程序的电池优化,因此意图不会被系统阻塞。因为有时系统会为了更好的电池寿命而阻止我们的工作。因此,如果您的应用程序在此设置下运行,则可以责怪系统。
还有一件事。您如何测试地理围栏?

谢谢你提供有关忽略电池优化的提示。我希望这不是必需的!我的测试方式是当我距离100米地理围栏1公里以上时,将应用程序移到前台,然后通过点击主屏幕按钮将其放到后台,等待1个小时以上,然后去到地理围栏中心,并等待长达15分钟来接收本地通知,即使我点亮屏幕,也从未触发。当我解锁手机并将应用程序移到前台时,它会立即触发。 - davidgyoung
你可以使用像Lockito这样的虚假位置应用进行测试。它们比真实位置更稳定。在真实位置下,你可能会得到错误的结果,因为地理围栏触发在真实场景中不太稳定。使用虚假位置可以确保测试结果的准确性。 - Mustafa Kuloğlu
我尝试了Lockito,在安卓9的Nokia 6.1+上重现了同样的问题。但在安卓7的华为P9上根本无法运行。 - davidgyoung

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接