Google Play服务提供ActivityRecognitionApi
,可让您检测各种用户活动(通过DetectedActivity
),例如用户是walking还是running。
是否有可能为开发和测试目的而模拟这些活动?
答案 0 :(得分:9)
是的,它是可能的,但仅限于模拟器(或根设备)。
例如,要模拟步行活动运行:
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
答案 1 :(得分:1)
没有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);