Android Geofence Transition PendingIntent 似乎没有 运行 (react-native bridge)

Android Geofence Transition PendingIntent seems not to run (react-native bridge)

我正在按照 android 指南 android guide 为地理围栏的 react-native 构建一个简单的原生桥。

但是我在进入或离开地理围栏时没有得到任何回应。似乎 PendingIntent / IntentService for Transitions 不正确 运行。

MyModule 看起来基本上是这样的。它还创建了 mGeofenceList,就像在文档中填充了来自 react-native 的数据一样。

我的模块:

public class MyModule extends ReactContextBaseJavaModule {  

  //Build geofences
  private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();
  }

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

  @ReactMethod
  public void startMonitoring() {
    mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
      .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
          Log.i(TAG, "Start Monitoring");
          postNotification("Start Monitoring", "Pressed Start Monitoring");
        }
      })
      .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
          Log.e(TAG, "Start Monitoring: " + e.getMessage());
        }
      });
  }
}

当运行startMonitoring()时,通知(开始监控)和日志都产生了,所以我认为错误不在这部分。

IntentService 看起来也很基础/类似于文档。 意向服务:

public class GeofenceTransitionsIntentService extends IntentService {
    private static final String TAG = "GeofenceService";
    private Handler handler;
    SharedPreferences sp;

    public GeofenceTransitionsIntentService(){
        super(TAG);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sp = PreferenceManager.getDefaultSharedPreferences(this);
        handler = new Handler();
        Log.i(TAG, "Intent created");
    }


    protected void onHandleIntent(Intent intent) {
        Log.i(TAG, "onHandleIntent");
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        if (geofencingEvent.hasError()) {
            String errorMessage = "Error Code: " + String.valueOf(geofencingEvent.getErrorCode());
            Log.e(TAG, errorMessage);
            return;
        }

        // Get the transition type.
        int geofenceTransition = geofencingEvent.getGeofenceTransition();

        // Test that the reported transition was of interest.
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
                geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

            // Get the geofences that were triggered. A single event can trigger
            // multiple geofences.
            List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

            // Get the transition details as a String.
            String geofenceTransitionDetails = getGeofenceTransitionDetails(
                    geofenceTransition,
                    triggeringGeofences
            );

            // Send notification and log the transition details.
            //sendNotification(geofenceTransitionDetails);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Enter/Exit", Toast.LENGTH_SHORT).show();
                }
            });
            Log.i(TAG, geofenceTransitionDetails);
        } else {
            // Log the error.
            Log.e(TAG, "Invalid transition");
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    /*
        Helpfunctions for logging
     */
    private String getGeofenceTransitionDetails(
            int geofenceTransition,
            List<Geofence> triggeringGeofences) {

        String geofenceTransitionString = getTransitionString(geofenceTransition);

        // Get the Ids of each geofence that was triggered.
        ArrayList<String> triggeringGeofencesIdsList = new ArrayList<>();
        for (Geofence geofence : triggeringGeofences) {
            triggeringGeofencesIdsList.add(geofence.getRequestId());
        }
        String triggeringGeofencesIdsString = TextUtils.join(", ",  triggeringGeofencesIdsList);

        return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
    }
    private String getTransitionString(int transitionType) {
        switch (transitionType) {
            case Geofence.GEOFENCE_TRANSITION_ENTER:
                return "entered Geofence";
            case Geofence.GEOFENCE_TRANSITION_EXIT:
                return "exit Geofence";
            default:
                return "unknown Transition";
        }
    }
}

但是 class 的输出中的 none 被生成了!

在我的本机模块的清单中,我添加了权限:

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

在使用这个模块的 TestApplication 的清单中我也添加了这个权限,在应用程序标签中我添加了

<service android:name="com.mymodule.GeofenceTransitionsIntentService" android:exported="false"/>

我无法在模块的清单中添加最后一行,因为它缺少应用程序标签并且没有 activity。我不确定这是不是正确的地方。

我在模拟器里测试,把location改成GPS数据播放列表

问题

  1. 如何验证 ServiceIntent 是 运行?我可以得到它的状态吗?
  2. 日志出现在哪里?在 com.TestApplication 或其他地方?

当然还有: 3.我的错误在哪里?

好的,回答我自己的问题,或具体的唯一问题 3:

上面的代码没有错误,或者至少在硬件设备上按预期工作。

那么,如何在模拟器上正确调试地理围栏?