[英]Testing Activity Recognition Transition API
I am currently developing an app which uses Activity Recognition Transition API (the new one, not the old, check the link below).我目前正在开发一个使用 Activity Recognition Transition API 的应用程序(新的,而不是旧的,请查看下面的链接)。 My question is, how can I test my app?我的问题是,如何测试我的应用程序? More exactly, how can I "manually" trigger transition events?更确切地说,我怎样才能“手动”触发转换事件? Do I really have to put my phone and laptop into my backpack and go for a ride on a bicycle to trigger the ON_BYCICLE/ACTIVITY_TRANSITION_ENTER event?我真的必须将手机和笔记本电脑放入背包并骑自行车去触发 ON_BYCICLE/ACTIVITY_TRANSITION_ENTER 事件吗? There must be an easier way :) Maybe using adb?必须有一个更简单的方法:) 也许使用 adb? https://developer.android.com/studio/test/command-line https://developer.android.com/studio/test/command-line
API documentation:https://developer.android.com/guide/topics/location/transitions API 文档:https ://developer.android.com/guide/topics/location/transitions
You can use following code to emulate the event您可以使用以下代码来模拟事件
var intent = Intent()
// Your broadcast receiver action
intent.action = BuildConfig.APPLICATION_ID + "TRANSITIONS_RECEIVER_ACTION"
var events: ArrayList<ActivityTransitionEvent> = arrayListOf()
// You can set desired events with their corresponding state
var transitionEvent = ActivityTransitionEvent(DetectedActivity.IN_VEHICLE, ActivityTransition.ACTIVITY_TRANSITION_ENTER, SystemClock.elapsedRealtimeNanos())
events.add(transitionEvent)
var result = ActivityTransitionResult(events)
SafeParcelableSerializer.serializeToIntentExtra(result, intent, "com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT")
activity?.sendBroadcast(intent)
I have created simple activity with two buttons in my application where I broadcast these events(Start and Stop) respectively.我在我的应用程序中创建了带有两个按钮的简单活动,我分别在其中广播这些事件(开始和停止)。 This helps me debug the application gracefully.这有助于我优雅地调试应用程序。
I don't know of any way to simulate a transition event from outside your app.我不知道有什么方法可以从您的应用程序外部模拟转换事件。 But within your app, you can construct a suitable Intent
and send it to your receiver.但是在您的应用程序中,您可以构建一个合适的Intent
并将其发送给您的接收器。
Add this method to any Context
(eg an Activity
or Service
): (Kotlin)将此方法添加到任何Context
(例如Activity
或Service
):(Kotlin)
class MyService: Service() {
fun sendFakeActivityTransitionEvent() {
// name your intended recipient class
val intent = Intent(this, MyReceiver::class.java)
val events: ArrayList<ActivityTransitionEvent> = arrayListOf()
// create fake events
events.add(
ActivityTransitionEvent(
DetectedActivity.ON_BICYCLE,
ActivityTransition.ACTIVITY_TRANSITION_ENTER,
SystemClock.elapsedRealtimeNanos()
)
)
// finally, serialize and send
val result = ActivityTransitionResult(events)
SafeParcelableSerializer.serializeToIntentExtra(
result,
intent,
"com.google.android.location.internal.EXTRA_ACTIVITY_TRANSITION_RESULT"
)
this.sendBroadcast(intent)
}
}
Substitute your desired recipient for MyReceiver
-- any subclass of BroadcastReceiver
should work.将您想要的接收者替换为MyReceiver
- BroadcastReceiver
的任何子类都应该工作。
Then call sendFakeActivityTransitionEvent()
when desired.然后在需要时调用sendFakeActivityTransitionEvent()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.