You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native安卓原生模块中Geofence无法触发的问题求助

React Native安卓原生模块中Geofence无法触发的问题求助

各位好,我现在在React Native的安卓原生模块(ReactContextBaseJavaModule)里实现Geofence功能,不确定是不是在这个类里初始化导致的问题。目前看起来所有流程都走通了:围栏能成功添加到API且没有报错,但从来不会触发进入/退出的回调。我已经翻了好几个教程和官方文档,还是没找到问题所在,麻烦大家帮忙排查一下!

我的实现代码

原生模块类(BackgroundFence.java)

public class BackgroundFence extends ReactContextBaseJavaModule {

private GeofencingClient geofencingClient;

private String TAG = "FENCE:BackgroundFence";

BackgroundFence(ReactApplicationContext context) {

super(context);

}

@ReactMethod(isBlockingSynchronousMethod = false)

public void createFence(double latitude, double longitude,int radius) {

Log.d("BackgroundFence", "Create fence for " + latitude +":"+longitude+" r:"+radius);

Activity activity = this.getReactApplicationContext().getCurrentActivity();

Context context = activity.getBaseContext();

geofencingClient = LocationServices.getGeofencingClient(context);

Geofence geofence = createGeofence(

latitude,

longitude,

radius,

Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT

);

GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()

.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)

.addGeofence(geofence)

.build();

List<Geofence> fences = geofencingRequest.getGeofences();

Log.d(TAG,"Fences: "+fences.size());

for (Geofence fence : fences) {

Log.d(TAG,"Fence: "+fence.getRequestId());

}

PendingIntent pendingIntent = getGeofencePendingIntent();

if (activity == null || ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

Log.d(TAG,"No activity or permission");

return;

}

geofencingClient.addGeofences(geofencingRequest, pendingIntent)

.addOnSuccessListener(activity, new OnSuccessListener<Void>() {

@Override

public void onSuccess(Void aVoid) {

Log.d(TAG, "onSuccess: Geofence Added...");

}

})

.addOnFailureListener(activity, new OnFailureListener() {

@Override

public void onFailure(@NonNull Exception e) {

Log.d(TAG, "onFailure: " + e.getMessage());

}

});

}

private PendingIntent getGeofencePendingIntent() {

ReactApplicationContext context = this.getReactApplicationContext();

Intent intent = new Intent(context, GeofenceBroadcastReceiver.class);

return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE );

}

public Geofence createGeofence(double latitude, double longitude, float radius, int transitionTypes) {

return new Geofence.Builder()

.setRequestId(generateRequestId())

.setCircularRegion(latitude, longitude, radius)

.setExpirationDuration(Geofence.NEVER_EXPIRE)

.setTransitionTypes(transitionTypes)

.build();

}

private String generateRequestId() {

return UUID.randomUUID().toString();

}

@Override

public String getName() {

return "BackgroundFence";

}

}

AndroidManifest.xml相关配置

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

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

广播接收器类(GeofenceBroadcastReceiver.java)

public class GeofenceBroadcastReceiver extends BroadcastReceiver {

private String TAG = "FENCE:GeofenceBroadcastReceiver";

@Override

public void onReceive(Context context, Intent intent) {

Log.d(TAG, "onReceive!");

GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

if(geofencingEvent==null) {

Log.d(TAG, "onReceive: geofencingEvent is null");

return;

}

if (geofencingEvent.hasError()) {

Log.d(TAG, "onReceive: Error receiving geofence event...");

return;

}

int transition = geofencingEvent.getGeofenceTransition();

if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) {

Log.d(TAG, "onReceive: ENTER");

} else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT) {

Log.d(TAG, "onReceive: EXIT");

}

}

}

备注:内容来源于stack exchange,提问作者somerandomusername

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 08:48:12