简体   繁体   中英

Why can't instantiate object from AudioManager with new operator?

I am novice with the android. I am learning how to make audio and control the voice of this audio with AudioManager class. But I have a simple question, according to the Documentation of the android developers it says that

AudoiManager is a public class AudioManager extends Object and to instantiate an object from this class Use Context.getSystemService(Context.AUDIO_SERVICE)

I got confused here Why we can't instantiate this class with the new operator and what does Context have to do with this?

Since this question concerns AudioManager object in Android, I would also like to add how you can achieve this using Kotlin as well since I had same challenge and I managed to sort using the below approach.

private lateinit var audioManager: AudioManager

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

    audioManager = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager
//...your other codes
}

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