Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use this and context in other class Android Kotlin?

Tags:

android

kotlin

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:

MainActivity:

class MainActivity : AppCompatActivity() {

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

        Permission().askMicrophonePermission()
    }
}

Permission:

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)
        }
    }
}
like image 207
Greg Avatar asked May 04 '18 09:05

Greg


People also ask

How do you pass context in Kotlin class?

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.

How do you pass a context in a constructor in Kotlin?

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.

What should I pass in context android?

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.

How do you use the companion object in Kotlin?

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.


2 Answers

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.

like image 95
IntelliJ Amiya Avatar answered Oct 09 '22 06:10

IntelliJ Amiya


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)
like image 27
Murtaza Khursheed Hussain Avatar answered Oct 09 '22 06:10

Murtaza Khursheed Hussain