背景地理围栏真的适用于Android 8+吗?

问题描述 投票:1回答:2

我有一个地理围栏应用程序,我试图移植到Android 8+上工作。我已经阅读了关于这个主题的tutorial,并转而使用compile 'com.google.android.gms:play-services-location:16.0.0'

当应用程序不在前台时,地理围栏输入事件永远不会触发。我等了两分钟以上,医生说这可能需要。我等了15分钟没有结果。只要我将应用程序带到地理围栏内的前台,它就会立即触发。

据我所知,在Android 8+上对后台服务有限制,但谷歌的教程说要使用一个应该在Android 8+上被阻止的IntentService。 HINDSIGHT EDIT:前面提到的教程是绝对错误的。不要跟着它。 IntentService不起作用。

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

对相关问题的回答here建议您必须使用BroadcastReceiver而不是IntentService,但我不明白为什么这会产生影响,因为Android 8在后台对IntentServices施加了与BroadcastReceivers相同的限制。 HINDSIGHT EDIT:这个链接的答案也是正确的。虽然隐式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:我已经看到在清单工作中声明的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不是这样)的方式?如果是这样,这将与Geofences一起使用吗? (剧透警报:是的,请阅读下面接受的答案。)

android location android-8.0-oreo geofencing android-geofence
2个回答
2
投票

你需要替换:

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/googlesamples/android-play-location/commit/5f83047c8a462d7c619f6275b624e219b4622322

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


2
投票

如果你相信自己通过this文章做正确的事情,你可以使用ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS设置。此设置禁用应用程序的电池优化。所以意图不是系统阻塞。因为有时系统阻止我们的工作以获得更好的电池寿因此,如果你的应用程序使用此设置运行,则可以归咎于系统。

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

© www.soinside.com 2019 - 2024. All rights reserved.