【问题标题】:Receiving geofence broadcasts with Android 26使用 Android 26 接收地理围栏广播
【发布时间】:2018-06-15 16:00:24
【问题描述】:

我正在注册接收地理围栏广播,它在 SDK 25 中运行良好。但在 SDK 27 中,这现在被破坏了。

我知道我无法通过清单文件注册以接收隐式广播,但我正在注册一个特定于类的广播,如下所示:

Intent geofencingIntent = new Intent("GEOFENCE_BROADCAST");
geofencingIntent.putExtra("ID", geofenceId);
geofencingIntent.setClass(context, GeofenceBroadcastReceiver.class);

PendingIntent geofencingPendingIntent = PendingIntent.getBroadcast(context, 1, geofencingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

final Task<Void> geofenceTask = mGeofencingClient.addGeofences(geofencingRequest, geofencingPendingIntent);

但是,我没有收到此广播。

关于为何未收到此“显式”广播以及解决方法的任何建议。即使应用未运行或未激活,接收广播也很重要。

我也无法理解删除此功能背后的逻辑。是的,它是通过防止应用程序不必要地唤醒来节省电池。但是在应用程序未运行时能够接收广播对我来说似乎是一个非常重要的功能。并且鼓励作业调度,至少只能设置为 15 分钟间隔,这似乎相当受限制。

我已检查以下答案,不认为这是重复的: Android 8.0 Oreo AlarmManager with broadcast receiver and implicit broadcast ban

Lifetime of BroadcastReceiver with regard to Android O changes

谢谢。

【问题讨论】:

标签: android broadcastreceiver android-geofence


【解决方案1】:

首先,如果您还没有在运行时注册广播,请注册它。 this 会帮助你。

如果您使用应用程序上下文注册广播,您会收到 只要应用程序正在运行,就会广播。

如果它失败了,那么通过 onFailedListener 你可以检查异常。

mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
        .addOnSuccessListener(this, new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // Geofences added
                // ...
            }
        })
        .addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Failed to add geofences
                // ...
            }
        });

如果可以,请尝试服务:

private PendingIntent getGeofencePendingIntent() {
        // Reuse the PendingIntent if we already have it.
        if (mGeofencePendingIntent != null) {
            return mGeofencePendingIntent;
        }
        Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
        // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
        // calling addGeofences() and removeGeofences().
        mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.
                FLAG_UPDATE_CURRENT);
        return mGeofencePendingIntent;
    }

我也无法理解删除此功能背后的逻辑。

docs中描述的很好

【讨论】:

    【解决方案2】:

    (代码)但是,我没有收到此广播。

    关于为何未收到此“显式”广播以及解决方法的任何建议。即使应用未运行或未激活,接收广播也很重要。

    我也无法理解删除此功能背后的逻辑。是的,它是通过防止应用程序不必要地唤醒来节省电池。但是在应用程序未运行时能够接收广播对我来说似乎是一个非常重要的功能。并且鼓励作业调度,至少只能设置为 15 分钟的间隔似乎相当限制

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多