Android What is context?
This question already has an answer here:
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
Source
In your scenario, GestureDetector
needs a Context
of an Activity
. Either you pass MyCurrentActivity.this
as a reference or button.getContext()
, both contexts belong to same Activity
.
Context
which you are getting from button.getContext()
is originally set when view is inflated.
The context is basically access to the application resources. When you get the context of the button, you are really getting a reference to the context of the activity that the button sits in, not the button itself. There are 4 types of Context
in Android:
Each of these Context types has different responsibilities and available resources. So the context usage here isn't to limit where you can touch, but rather that you can work with the UI. If you want to limit where the gesture detector is working, you would simply attach the detector to the view. You would still need the Activity context to create the detector though.
I found this article on the different context types to be very helpful:
https://possiblemobile.com/2013/06/context/
链接地址: http://www.djcxy.com/p/91370.html上一篇: android中的上下文是什么?
下一篇: Android什么是上下文?