开发者

How can I tell when my instrumented application is in front?

开发者 https://www.devze.com 2023-03-12 05:42 出处:网络
My instrumentation is launching an activity to instrument it, but I can\'t seem to find an elegant way开发者_运维问答 to tell when it\'s safe to start sending it my MotionEvent\'s. At the moment, I\'m

My instrumentation is launching an activity to instrument it, but I can't seem to find an elegant way开发者_运维问答 to tell when it's safe to start sending it my MotionEvent's. At the moment, I'm continually sending it MotionEvent's, and catching the SecurityException's that get thrown when the wrong application receives them.

The problem with this approach is that when the first event gets through, the application responds to it, so this method is not without side effects...

I was thinking of trying to do something with UID's, but I don't know where to look for that.


You might be able to leverage the standard Android lifecycle methods that get called on your Activity. You could set an 'infront' flag in onResume(), and clear it in onPause(). Then your instrumentation test just checks that flag to know when it's safe to send MotionEvents.

If you don't want to pollute your app by putting in code specifically for test purposes, create a subclass, override onResume/onPause, set the flags, and call super.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号