背景地理围栏真的可以在Android 8+上运行吗?

时间:2019-03-01 17:57:57

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

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

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

我知道在Android 8+上对后台服务有限制,但是Google的教程说要使用IntentService,应该在Android 8+上阻止它。 注意事项:上述教程绝对是错误的。不要遵循它。 IntentService无法正常工作。

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

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

是否真的有可能在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:根据Google记录的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一起使用吗? ((扰流板警报:是的!请阅读下面接受的答案。)

2 个答案:

答案 0 :(得分:1)

如果您对this文章所做的一切都正确无误,则可以使用ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS设置。此设置禁用应用程序的电池优化。因此,意图不会被系统阻止。因为有时系统会阻塞我们的工作以延长电池寿命。因此,如果您的应用使用此设置运行,则可以归咎于系统。

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

答案 1 :(得分:1)

您需要替换:

"..."

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

对于支持Android 8,由于this answer(服务不能在后台运行),因此应使用BroadcastReceiver接收地理围栏意图。

有关详情,请参见:Background Execution Limits

google示例如何使用地理围栏:https://github.com/googlesamples/android-play-location/commit/5f83047c8a462d7c619f6275b624e219b4622322