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.
精彩评论