rokkonet

PC・Androidソフトウェア・アプリの開発・使い方に関するメモ

android開発 ActivityResultContractsとIntentで他アプリを起動するコード例

2023 Mar. 05.

端末のストレージを読み込む権限を取得し、Intent.ACTION_OPEN_DOCUMENT_TREEで端末組み込みファイラーを開き、ディレクトリを取得するコード


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.sytes.rokkosan.myfilerchooser">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <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.MyFilerChooser">
        <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>


build.gradle(:app)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 33

    defaultConfig {
        applicationId "net.sytes.rokkosan.myfilerchooser"
        minSdk 30
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textViewGot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/buttonStart"
        />

    <Button
        android:id="@+id/buttonStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start_button_title"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textViewGot" />
</androidx.constraintlayout.widget.ConstraintLayout>


strings.xml

<resources>
    <string name="app_name">MyFilerChooser</string>
    <string name="start_button_title">Start</string>
    <string name="failed_permission">Failed_to_get_permission</string>
    <string name="ok">OK</string>
    <string name="reason_for_permission">ストレージファイル読み込みに読込権限の許可が必要です。設定を開きますか?</string>
    <string name="dismiss">Dismiss</string>
</resources>


MainActivity.kt

// 2023 Jan. 15.
// Ryuichi Hashimoto.

package net.sytes.rokkosan.myfilerchooser

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {
    private val myPermission = Manifest.permission.READ_EXTERNAL_STORAGE
    private var isCheckedNotAskAgain: Boolean = false
    private val sharedPref: SharedPreferences by lazy {
        this.getSharedPreferences("myPref", Context.MODE_PRIVATE)
    }
    val textViewGot: TextView by lazy { findViewById<TextView>(R.id.textViewGot) }

    // registerForActivityResult()を定義してlaunch()を呼び出すと、
    // パーミッションが許可されていない時はユーザーにパーミッション許可を促すダイアログが表示される。
    // パーミッションが「許可された時」「許可されなかった時」のコールバック関数を記述する
    private val permisLauncher = registerForActivityResult(
        ActivityResultContracts.RequestPermission() ) {
            granted ->
        if (granted) {
            // パーミッションが取得されている時
            // パーミッション取得後にすべき処理に移る
            isCheckedNotAskAgain = false
            with (sharedPref.edit()) {
                putBoolean("isCheckedNotAskAgain", isCheckedNotAskAgain)
                apply()
            }
            myWorkWithPermission()
        } else {
            // パーミッションを取得していない
            if (shouldShowRequestPermissionRationale(myPermission)) {
                // パーミッション取得を拒否され、
                // 「今後表示しない」は選択されていない
                isCheckedNotAskAgain = false
                with (sharedPref.edit()) {
                    putBoolean("isCheckedNotAskAgain", isCheckedNotAskAgain)
                    apply()
                }
            } else {
                // パーミッション取得を拒否され、
                // 「今後表示しない」が選択されて再リクエストも拒否されている
                isCheckedNotAskAgain = true
                with (sharedPref.edit()) {
                    putBoolean("isCheckedNotAskAgain", isCheckedNotAskAgain)
                    apply()
                }
            }
            myWorkWithoutPermission()
        }
    }

    private val startFilerForResult =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                result: ActivityResult? ->
            if (result?.resultCode == Activity.RESULT_OK) {
                result.data?.let { data: Intent ->
                    val value = data.data.toString()
                    Toast.makeText(this, value, Toast.LENGTH_LONG).show()
                }
            }
        }

    // パーミッション取得後に行うこと
    private fun myWorkWithPermission() {
        val buttonStart: Button = findViewById<Button>(R.id.buttonStart)
        buttonStart.setOnClickListener {
            val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
            startFilerForResult.launch(intent)
        }
    }

    // パーミッション取得しなかった時に行うこと
    private fun myWorkWithoutPermission(){
        textViewGot.text = getString(R.string.failed_permission)
    }

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

        // ストレージ読み込み権限取得
        isCheckedNotAskAgain = sharedPref.getBoolean("isCheckedNotAskAgain", false)
        if ( ContextCompat.checkSelfPermission(this, myPermission) ==
            PackageManager.PERMISSION_GRANTED) {
            // 既にパーミッションが取得されている時
            // パーミッション取得後にすべき処理に移る
            isCheckedNotAskAgain = false
            with (sharedPref.edit()) {
                putBoolean("isCheckedNotAskAgain", isCheckedNotAskAgain)
                apply()
            }
            myWorkWithPermission()
        } else {
            // パーミッション未取得
            if (isCheckedNotAskAgain) {
                // 過去に権限取得を拒否され、「今後表示しない」が選択されて再リクエストも拒否されている
                // ダイアログでユーザーに説明し、ユーザーが望めば設定画面を表示する
                val openConfigDialogFragment = OpenConfigDialogFragment()
                openConfigDialogFragment.show(supportFragmentManager, "simple")

                // ダイアログ後の処理
                if ( ContextCompat.checkSelfPermission(this, myPermission) ==
                    PackageManager.PERMISSION_GRANTED) {
                    // (設定画面で)パーミッションを取得している
                    isCheckedNotAskAgain = false
                    with (sharedPref.edit()) {
                        putBoolean("isCheckedNotAskAgain", isCheckedNotAskAgain)
                        apply()
                    }
                    myWorkWithPermission()
                } else {
                    // パーミッション未取得。
                    // shouldShowRequestPermissionRationale()に応じて
                    // isCheckedNotAskAgainを設定し保存する
                    if (shouldShowRequestPermissionRationale(myPermission)) {
                        // 「今後表示しない」は選択されていない
                        isCheckedNotAskAgain = false
                        with (sharedPref.edit()) {
                            putBoolean("isCheckedNotAskAgain", isCheckedNotAskAgain)
                            apply()
                        }
                    } else {
                        // 「今後表示しない」が選択されている
                        isCheckedNotAskAgain = true
                        with (sharedPref.edit()) {
                            putBoolean("isCheckedNotAskAgain", isCheckedNotAskAgain)
                            apply()
                        }
                    }
                    myWorkWithoutPermission()
                }
            } else {
                // パーミッションを取得しておらず、
                // 過去に権限取得で再リクエストの拒否はしていない。
                permisLauncher.launch(myPermission)
            }
        }
    }
}


OpenConfigDialogFragment.kt

package net.sytes.rokkosan.myfilerchooser

import android.app.Dialog
import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment

class OpenConfigDialogFragment: DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            builder.setMessage(R.string.reason_for_permission)
                .setPositiveButton(R.string.ok,
                    DialogInterface.OnClickListener { dialog, id ->
                        // このアプリの設定画面への暗黙的インテントを用意する
                        val packageName = context?.applicationContext?.packageName
                        val intent = Intent(
                            Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                            Uri.parse("package:${packageName}")
                        )
                        // 新しいタスクで設定画面を開く
                        //   https://developer.android.google.cn/guide/components/activities/tasks-and-back-stack?hl=ja
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        startActivity(intent)
                    })
                .setNegativeButton(R.string.dismiss,
                    DialogInterface.OnClickListener { dialog, id ->
                        // User cancelled the dialog
                    })
            // Create the AlertDialog object and return it
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}