Android Permission | ActivityResultLauncher

MainActivity

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {
private lateinit var pLauncher: ActivityResultLauncher<Array<String>>

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

private fun checkCameraPermission(){
when{
ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED ->{
Toast.makeText(this,"Camera run", Toast.LENGTH_LONG).show()
}

else -> {
pLauncher.launch(arrayOf(Manifest.permission.CAMERA))
}
}
}

private fun registerPermissionListener(){
pLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()){
if(it[Manifest.permission.CAMERA] == true){
Toast.makeText(this,"Camera run", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this,"Permission denied", Toast.LENGTH_LONG).show()
}
}
}


}

Добавить комментарий

Ваш адрес email не будет опубликован.