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

余生长醉 提交于 2019-12-12 08:43:51

问题


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

Is it possible to mock these activities for development and testing purposes?


回答1:


Yes, it's possible, but only on the emulator (or a rooted device).

For example, to simulate the walking activity run:

adb root
adb shell am broadcast -a com.google.gservices.intent.action.GSERVICES_OVERRIDE -e 'location:mock_activity_type' 'WALKING'

And then restart Google Play Services (or restart the device):

adb shell ps -A | grep com.google.android.gms.persistent | awk '{print $2}' | xargs adb shell kill



回答2:


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<ActivityTransitionEvent> 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);


来源:https://stackoverflow.com/questions/45292256/is-it-possible-to-simulate-detected-activities-for-the-activityrecognitionapi-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!