简体   繁体   English

Android Geofence Transition PendingIntent似乎未运行(反应本机桥)

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

I am following the android guide android guide to build a simple native bridge for react-native for geofencing. 我正在按照android guide android guide来构建一个简单的本机桥,用于geofencing的react-native。

But I do not get any response when entering or leaving a geofence. 但是进入或离开地理围栏时我没有任何反应。 It seems like the PendingIntent / IntentService for Transitions is not running properly. 似乎PendingIntent / IntentService for Transitions无法正常运行。

MyModule looks basically like this. MyModule基本上看起来像这样。 It also creates mGeofenceList like in the docs populated with data from react-native. 它还像在文档中创建mGeofenceList一样,其中填充了react-native的数据。

MyModule: MyModule的:

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());
        }
      });
  }
}

When running startMonitoring() , the notification (Start Monitoring) and the log gets produced, so I assume that the error is not in this part. 运行startMonitoring() ,将生成通知(“开始监视”)和日志,因此我认为该部分中没有错误。

The IntentService looks also pretty basic /similar to the docs. IntentService看起来也很基本/类似于文档。 IntentService: 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";
        }
    }
}

But none of the outputs of this class gets produced! 但是此类的任何输出都不会产生!

In the manifest of my native module I added the permission: 在本机模块的清单中,我添加了权限:

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

And in the manifest of the TestApplication that uses this module I added this permission as well and in the application tag I added 在使用该模块的TestApplication的清单中,我也添加了此权限,并在我添加的应用程序标签中

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

I could not add this last line in the module's manifest, cause it was missing the application tag and has no activity. 我无法在模块清单中添加最后一行,因为它缺少应用程序标签并且没有活动。 I am not sure if this is the right place. 我不确定这是否是正确的地方。

I am testing in the emulator and change the location to a list of GPS data playback. 我正在模拟器中进行测试,并将位置更改为GPS数据播放列表。

Questions 问题

  1. How can I verify that the ServiceIntent is running? 如何验证ServiceIntent是否正在运行? Can I get Status of it? 我可以得到它的状态吗?
  2. Where does the logs appear? 日志出现在哪里? In com.TestApplication or somewhere else? 在com.TestApplication或其他地方?

and of course: 3. Where is my error? 当然:3.我的错误在哪里?

Ok, answering my own question, or specific only question 3: 好吧,回答我自己的问题,或仅回答特定问题3:

The code above has no error, or at least and works as expected on a hardware device. 上面的代码没有错误,或者至少没有错误,并且可以在硬件设备上正常工作。

So, how to properly debug Geofencing on an emulator? 那么,如何在模拟器上正确调试Geofencing?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM