简体   繁体   中英

What does "this" refer to in Android checkSelfPermission()?

I'm wondering what the this keyword refers to in the below code (the code block is to request permission to access user location).

class RequiresLocation : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_requires_location)

        turnOnLocationButton.setOnClickListener {
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED){
                ...
            }
        }
    }
}

I checked the Android docs for checkSelfPermission() and it has this:

int checkSelfPermission (Context context, 
                String permission)

What does the context here specifically refer to? Is it the application as a whole not the activity?

Context is 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.

Different methods by which you can get context

  1. getApplicationContext()
  2. getContext()
  3. getBaseContext()
  4. this (when in the activity class)

this -> refers to the context of the current activity.

I'm wondering what the this keyword refers to in the below code

In your code snippet

ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED

the keyword this refers to the current Activity instance.

For those among us who are used to writing Java code: in this case Kotlin is different from Java.

In Java, you would have to write RequiresLocation.this once you're "inside" the scope of a View.OnClickListener .

In Kotlin, simply this will do. But if you are working with Android Studio or IntelliJ Idea and continue typing by entering a @ right after the this , then the code completion will offer you this@RequiresLocation , so you can be sure that it is indeed the correct this .

What does the Context parameter in checkSelfPermission() refer to?

You can pass in any Context - an Activity , the Application , but also some type of Service (note that Application and Service both extend from ContextWrapper which according to the docs has seven direct and more than 40 indirect subclasses, one of them is Activity . All of them are valid arguments to checkSelfPermission() .)

It refers to the current instance of the RequiresLocation class.

Fully qualified would read more clearly like: RequiresLocation.this

So, as you noticed the signature of checkSelfPermission requires a Context , and " this " (the instance of RequiresLocation) can be passed as such context parameter because all Activities derive from Context . Take into account that since RequiresLocation derives from AppCompatActivity , such class is also a Context .

Context refers to current activity state. We use context for getting information of current activity state. You can also refer the below link for getting detail information about Context . https://blog.mindorks.com/understanding-context-in-android-application-330913e32514

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM