Is it possible to simulate detected activities for the ActivityRecognitionApi for testing purposes?

前端 未结 2 521
我在风中等你
我在风中等你 2021-01-02 06:35

Google Play Services provides an ActivityRecognitionApi that lets you detect various user activities (via DetectedActivity) such as if the user is walking or running.

<
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 07:15

    It's possible to do it without the adb commands. Create and send an intent with the correct extra.

    Add the transitions you need to a list and add that list to the constructor of an ActivityTransitionResult object. To create the extra, use SafeParcelableSerializer.serializeToIntentExtra with the key "com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT"

    I have used this code to simulate the transition from still to walking.

    Intent intent = new Intent();
    intent.setAction("MYLISTENINGACTION");
    List events = new ArrayList<>();
    ActivityTransitionEvent transitionEvent;
    transitionEvent = new ActivityTransitionEvent(DetectedActivity.STILL, 
       ActivityTransition.ACTIVITY_TRANSITION_EXIT, SystemClock.elapsedRealtimeNanos());
    events.add(transitionEvent);
    transitionEvent = new ActivityTransitionEvent(DetectedActivity.WALKING, 
       ActivityTransition.ACTIVITY_TRANSITION_ENTER, SystemClock.elapsedRealtimeNanos());
    events.add(transitionEvent);
    ActivityTransitionResult result = new ActivityTransitionResult(events);
    SafeParcelableSerializer.serializeToIntentExtra(result, intent, 
       "com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT");
    sendBroadcast(intent);
    

提交回复
热议问题