Google Play Services 提供了一个 ActivityRecognitionApi
,可以通过DetectedActivity
检测不同的用户活动,例如用户是步行还是跑步等。
是否有可能模拟这些活动以进行开发和测试?
Google Play Services 提供了一个 ActivityRecognitionApi
,可以通过DetectedActivity
检测不同的用户活动,例如用户是步行还是跑步等。
是否有可能模拟这些活动以进行开发和测试?
是的,这是可能的,但只能在模拟器上(或经过root的设备上)。
例如,要模拟步行活动运行:
adb root
adb shell am broadcast -a com.google.gservices.intent.action.GSERVICES_OVERRIDE -e 'location:mock_activity_type' 'WALKING'
然后重新启动Google Play服务(或重新启动设备):
adb shell ps -A | grep com.google.android.gms.persistent | awk '{print $2}' | xargs adb shell kill
可以不使用adb命令来完成此操作。创建并发送一个带有正确额外信息的意图。
将所需的转换添加到列表中,并将该列表添加到ActivityTransitionResult对象的构造函数中。要创建额外信息,请使用SafeParcelableSerializer.serializeToIntentExtra函数和键“com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT”。
我已经使用这段代码模拟了从静止到步行的过渡。
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);
com.google.android.gms.common.internal.safeparcel.SafeParcelableSerializer;
,但是出现了错误,请问有没有解决方法? - Rajat Singh