由于某种原因,当通过IntentService
发出事件时,Javascript方面没有任何反应。这是我在做什么:
使用IntentService
方法创建一个onHandleIntent
,如下所示:
@Override
protected void onHandleIntent(@Nullable final Intent intent) {
if (intent != null) {
logger.info("Broadcasting event");
intent.setAction(ACTION);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
将IntentService
注册到com.google.android.gms.location.GeofencingClient
:
Intent intent = new Intent(getReactApplicationContext().getBaseContext(), BoundaryEventIntentService.class);
this.mBoundaryPendingIntent = PendingIntent.getService(getReactApplicationContext().getBaseContext(), 0, intent, PendingIntent.
FLAG_UPDATE_CURRENT);
为BroadcastReceiver
创建IntentService
,以便将消息广播到:
public class GeofenceEventBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
// handle errors
}
switch (geofencingEvent.getGeofenceTransition()) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
Log.i(TAG, "Enter geofence event detected. Sending event.");
WritableArray writableArray = Arguments.createArray();
for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) {
writableArray.pushString(geofence.getRequestId());
}
sendEvent(ON_ENTER, writableArray);
break;
// handle other events e.g. GEOFENCE_TRANSITION_EXIT
}
}
}
最后是我的sendEvent
方法:
private void sendEvent(String event, Object params) {
Log.i(TAG, "Sending events " + event);
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(event, params);
Log.i(TAG, "Sent events");
}
我的BroadcastReceiver
是我的ReactContextBaseJavaModule
的内部类,因此它可以访问我的react应用程序上下文和sendEvent
方法。一切都成功触发,但其中的javascript端未收到事件。我意识到这是很多代码,并且我可能没有包含所有需要的上下文。如果需要更多上下文,可以在here中找到该库。
答案 0 :(得分:0)
可能的原因是您使用的是Intent服务,该服务会受到打ze模式的影响。更新为使用作业意图服务而不是意图服务。
https://developer.android.com/reference/android/support/v4/app/JobIntentService
Android背景位置限制 为了节省电池,用户体验和系统健康,后台应用在运行Android 8.0的设备上使用时,接收位置更新的频率较低。此行为更改会影响所有接收位置更新的应用,包括Google Play服务。
这些更改会影响以下API:
融合位置提供者(FLP) 地理围栏 GNSS测量 位置经理 Wi-Fi管理器
阅读此链接,了解在8.0中所做的更改
https://developer.android.com/about/versions/oreo/android-8.0-changes
注意:IntentService是一项服务,因此受后台服务的新限制。因此,在定位Android 8.0或更高版本时,许多依赖IntentService的应用程序无法正常运行。因此,Android支持库26.0.0引入了一个新的JobIntentService类,该类提供与IntentService相同的功能,但是在Android 8.0或更高版本上运行时使用作业而不是服务。