0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

実行時の権限をリクエストする(Android Studio Ladybug Feature Drop | 2024.2.2)

Last updated at Posted at 2025-01-17

読めばできる公式

AndroidManifestは変わらない

AndroidManifest
<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />
        

ユーザー許可

簡単

mainActivity

import android.Manifest.permission.CAMERA
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE //←パーミッションをインポートする

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //省略
        ActivityCompat.requestPermissions(
                this, arrayOf(
                    WRITE_EXTERNAL_STORAGE,
                    CAMERA
                    ), 1
            )
    }
}

丁寧

MainActivity
import android.Manifest.permission.CAMERA
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE //←パーミッションをインポートする

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //省略
        when {
        //一つ一つチェックするしかない…??調査中
            ContextCompat.checkSelfPermission(
                this,
                WRITE_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_GRANTED -> {
                Log.e("tag","すでに権限は与えられてます")
                // You can use the API that requires the permission.
                //↑と書かれているが一度権限を与えればこのプロジェクト内では使える
                //既に許可済みならここを通り下記ダイアログ表示は発生しない
            }
             ActivityCompat.shouldShowRequestPermissionRationale(
             //ダイアログでファルダを許可しなかった場合の処理
             //許可してほしいので許可するまでリクエストするという意思
             this,WRITE_EXTERNAL_STORAGE) -> {
             requestPermissions(
                    arrayOf(WRITE_EXTERNAL_STORAGE),
                    100)
            }
            else -> {
            //初めてのリクエスト
            //arrayOfなのでまとめてできる
                requestPermissions(
                    arrayOf(WRITE_EXTERNAL_STORAGE,CAMERA),
                    100)
            }
    }
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        Log.d("p",permissions[0])//パーミッション名で指定するのもあり
        if ((grantResults.isNotEmpty() &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED)){
            Log.e("tag","利用できる")
            }
        else {
            Log.e("tag","許可がないため利用できない")
        }
        //許可があれば0,なければ-1
        Log.e("tag", ContextCompat.checkSelfPermission(this,WRITE_EXTERNAL_STORAGE).toString())
        Log.e("tag", ContextCompat.checkSelfPermission(this, CAMERA).toString())
        return
    }
}

Screenshot_20250117-105937.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?