是否可以为ActivityTcognitionApi模拟检测到的活动以进行测试?

Ada*_*amK 7 android google-play-services activity-recognition

Google Play服务提供了一种ActivityRecognitionApi可让您检测各种用户活动(通过DetectedActivity)的功能,例如用户是在行走还是在跑步.

为开发和测试目的,是否可以模拟这些活动?

Ada*_*amK 14

是的,这是可能的,但仅限于模拟器(或根设备).

例如,要模拟步行活动运行:

adb root
adb shell am broadcast -a com.google.gservices.intent.action.GSERVICES_OVERRIDE -e 'location:mock_activity_type' 'WALKING'
Run Code Online (Sandbox Code Playgroud)

然后重新启动Google Play服务(或重启设备):

adb shell ps -A | grep com.google.android.gms.persistent | awk '{print $2}' | xargs adb shell kill
Run Code Online (Sandbox Code Playgroud)


小智 5

可以在没有 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);
Run Code Online (Sandbox Code Playgroud)