是否可以为 ActivityRecognitionApi 模拟检测到的活动以进行测试?
Is it possible to simulate detected activities for the ActivityRecognitionApi for testing purposes?
Google Play 服务提供 ActivityRecognitionApi
that lets you detect various user activities (via DetectedActivity
) such as if the user is walking or running.
是否可以出于开发和测试目的模拟这些活动?
是的,这是可能的,但仅限于模拟器(或已获得 root 权限的设备)。
比如模拟走路activity运行:
adb root
adb shell am broadcast -a com.google.gservices.intent.action.GSERVICES_OVERRIDE -e 'location:mock_activity_type' 'WALKING'
然后重启Google播放服务(或重启设备):
adb shell ps -A | grep com.google.android.gms.persistent | awk '{print }' | xargs adb shell kill
没有 adb 命令也可以做到这一点。创建并发送带有正确 extra 的意图。
将您需要的转换添加到列表中,并将该列表添加到 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);
Google Play 服务提供 ActivityRecognitionApi
that lets you detect various user activities (via DetectedActivity
) such as if the user is walking or running.
是否可以出于开发和测试目的模拟这些活动?
是的,这是可能的,但仅限于模拟器(或已获得 root 权限的设备)。
比如模拟走路activity运行:
adb root
adb shell am broadcast -a com.google.gservices.intent.action.GSERVICES_OVERRIDE -e 'location:mock_activity_type' 'WALKING'
然后重启Google播放服务(或重启设备):
adb shell ps -A | grep com.google.android.gms.persistent | awk '{print }' | xargs adb shell kill
没有 adb 命令也可以做到这一点。创建并发送带有正确 extra 的意图。
将您需要的转换添加到列表中,并将该列表添加到 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);