<permission>
ํ๊ทธ๋ก ํน์ ์ปดํฌ๋ํธ์ ๊ถํ์ ์ค์ ํ ์ ์๋ค. <uses-permission>
ํ๊ทธ๋ฅผ ์ด์ฉํ๋ฉด, A์ ์ปดํฌ๋ํธ์ ๋ํ ์ ๊ทผ ๊ถํ์ ์ป์ ์ ์๋ค.
AndroidManifest.xml ํ์ผ์ ๋ค์ด๊ฐ๋, ํน์ ์ปดํฌ๋ํธ๋ฅผ ๋ณดํธํ๊ธฐ ์ํ ์ค์
๊ถํ์ด ์ค์ ๋ ์ปดํฌ๋ํธ๋ฅผ ์ธ๋ถ ์ฑ์์ ์ฌ์ฉํ๋ ค๊ณ ํ ๋๋ <uses-permission> ํ๊ทธ๋ฅผ ์ด์ฉํ๋ฉด ๋๋ค.
์ธ๋ถ ์ฑ์์ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ ๋ ํ์ํ ํผ๋ฏธ์ ๋ง๊ณ ๋, ์์คํ ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ปดํฌ๋ํธ๋ฅผ ๋ณดํธํ๊ธฐ ์ํด ์๊ตฌํ๋ ํผ๋ฏธ์ ๋ค์ด ์๋ค. ํด๋น ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ค๋ฉด ๋ง์ฐฌ๊ฐ์ง๋ก <uses-permission> ํ๊ทธ๋ฅผ ๊ฑธ์ด์ค์ผ ํ๋ค.
requestPermissions() ํจ์๋ฅผ ํธ์ถํ๋ฉด ์ ์ ์๊ฒ ๊ถํ ๋ถ์ฌ๋ฅผ ์์ฒญํ๋ ์์คํ ๋ค์ด์ผ๋ก๊ทธ๊ฐ ๋ฌ๋ค.
API Level 31๋ถํฐ ์ฌ์ฉ ๊ฐ๋ฅ
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.c51">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLab">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.kt
package com.tutorial.c51
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val status = ContextCompat.checkSelfPermission(
this,
"android.permission.ACCESS_FINE_LOCATION"
)
if(status == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "granted...", Toast.LENGTH_SHORT).show()
}else{
// ์์ฒญ ๋ค์ด์ผ๋ก๊ทธ ๋์ฐ๊ธฐ
ActivityCompat.requestPermissions(
this,
arrayOf("android.permission.ACCESS_FINE_LOCATION"),
100
)
}
}
// ์์ฒญ ๋ค์ด์ผ๋ก๊ทธ ์๋ต ๊ฒฐ๊ณผ์ ๋ํ ์ฒ๋ฆฌ
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "granted...", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this, "denied...", Toast.LENGTH_SHORT).show()
}
}
}
package com.tutorial.c51
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
){
if(it){
Toast.makeText(this, "granted...", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this, "denied...", Toast.LENGTH_SHORT).show()
}
}
val status = ContextCompat.checkSelfPermission(
this,
"android.permission.ACCESS_FINE_LOCATION"
)
if(status == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "granted...", Toast.LENGTH_SHORT).show()
}else{
// ์์ฒญ ๋ค์ด์ผ๋ก๊ทธ ๋์ฐ๊ธฐ
// test1...
// ActivityCompat.requestPermissions(
// this,
// arrayOf("android.permission.ACCESS_FINE_LOCATION"),
// 100
// )
// test2...
requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}
}
// ์์ฒญ ๋ค์ด์ผ๋ก๊ทธ ์๋ต ๊ฒฐ๊ณผ์ ๋ํ ์ฒ๋ฆฌ
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "granted...", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this, "denied...", Toast.LENGTH_SHORT).show()
}
}
}