How to use this and context in other class Android Kotlin?
You can use below
For KOTLIN
this
replaced bythis@MainActivity
You should set
Permission().askMicrophonePermission(this@MainActivity)
Then Pass Context.
fun askMicrophonePermission(context: Context)
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system.
FYI
In a member of a class, this
refers to the current object of that class.
You can harness the power of companion object
in Kotlin and create static methods like Java.
private companion object {
fun askMicrophonePermission(context: Context) {
val userMicrophonePermissionAgreeCode = 1
val currentMicrophonePermission = ActivityCompat.checkSelfPermission(context, android.Manifest.permission.RECORD_AUDIO)
if (currentMicrophonePermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context, arrayOf(android.Manifest.permission.RECORD_AUDIO), userMicrophonePermissionAgreeCode)
}
}
}
And then you use it like
ClassName.askMicrophonePermission(this@YouActivity)