2

我正在尝试Geofence触发 aBroadcastReceiver并且BroadcastReceiver具有从意图读取数据并对意图发送的数据进行操作的代码。我BroadCastReceiver的是GeoLocationBroadCastReceiver。当我的活动打开并且应用程序处于后台时,我正在调用此代码。

public PendingIntent getPendingIntent(Context context){
   Intent i = new Intent(context, GeoLocationBroadCastReceiver.class);
   return PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
}

PendingIntentGeofencingRequest.Builder(此数据在 addGeoFence 方法中返回)一起传递给 addGeofences 方法。

GeofencingClient client = LocationServices.getGeofencingClient(context);
 client.addGeofences(addGeoFence(100, lat, long, true), getPendingIntent(context))
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.e(TAG,"Added from location geofence");
                    }
                });

当我调用上述代码时,地理围栏已成功添加。

Manifest.xml 文件中的接收方声明:

 <receiver
        android:name=".LocationMagic.GeoLocationBroadCastReceiver"
        android:enabled="true"
        android:exported="true"/>

问题:我没有收到广播,也没有调用接收器。我看到谷歌建议的方法是使用广播来克服服务的背景限制。我也知道广播限制(列入白名单和列入黑名单)。但根据谷歌代码实验室,建议的方法是使用广播。谷歌代码实验室链接在这里

来自谷歌代码实验室:

以 O 为目标的应用程序还受到后台启动服务的限制。因此,面向 O 的应用程序在请求位置更新时不应使用 PendingIntent.getService()。相反,他们应该使用 PendingIntent.getBroadcast()。

4

0 回答 0