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

11
2个回答

17

是的,这是可能的,但只能在模拟器上(或经过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

有更多细节吗?我在哪里可以找到更多文档?我想模拟从步行状态到车辆状态的切换... - Oleksandr Kucherenko
1
抱歉,不幸的是这没有官方记录。我直接从团队那里收到了这些指示,所以应该起作用。您只需将“步行”替换为“车辆”即可尝试进行切换。 - AdamK
@AdamK 上述方法对我无效,我的日志中确实显示了物理活动转换,但我无法模拟它。 - Uzair
我在模拟器和设备上都尝试了一下(测试requestActivityTransitionUpdates)。但是两者都不起作用 :( - android developer
不幸的是,这是2017年的内容。我不知道这个标志是否仍然受支持。如果它不起作用,那很可能不会 :( - AdamK
1
@AdamK 有其他的选择吗? - android developer

6

可以不使用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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接