I want to create a askMicrophonePermission
function in Permission.class
.
And write Permission().askMicrophonePermission()
in onCreate()
.
I don't known how to change this so that ActivityCompat.checkSelfPermission
and ActivityCompat.requestPermissions
can run in the
Permission().askMicrophonePermission()
.
Here is my code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Permission().askMicrophonePermission()
}
}
class Permission {
fun askMicrophonePermission(){
val userMicrophonePermissionAgreeCode = 1
val currentMicrophonePermission = ActivityCompat.checkSelfPermission(_________,android.Manifest.permission.RECORD_AUDIO)
if (currentMicrophonePermission != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(_______________, arrayOf(android.Manifest.permission.RECORD_AUDIO), userMicrophonePermissionAgreeCode)
}
}
}
You are free to pass a Context to a class that its not attached to an Activity in any way you like. Passing it through the class's constructor is a good practice (dependency injection), but only in the case where your class needs a Context to fully function correctly.
Define an interface with the same factory function and two objects for the scopes. Define a function that takes the scope and the initializer block. Now you can use the useScope -Function and within the block the right factory function is invoked. Save this answer.
It is used to return the Context which is linked to the Application which holds all activities running inside it. When we call a method or a constructor, we often have to pass a Context and often we use “this” to pass the activity Context or “getApplicationContext” to pass the application Context.
To create a companion object, you need to add the companion keyword in front of the object declaration. The output of the above code is “ You are calling me :) ” This is all about the companion object in Kotlin. Hope you liked the blog and will use the concept of companion in your Android application.
You can use below
For KOTLIN
this
replaced by this@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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With