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方式不受其影响)?如果是这样,那么这是否适用于地理围栏?(剧透:是的!请阅读下面接受的答案。)