0
0

(Android / Kotlin) ホーム画面に設定する方法 (メモ)

Posted at

最終変更日: 2024/09/11
最終検証日: 2024/09/11

設定方法

  1. パーミッションの設定 (app/src/main/AndroidManifest.xml)
  2. 確認画面の表示 (app/src/main/java/{スラッシュ区切りのID}/MainActivity.kt)

パーミッションの設定

app/src/main/AndroidManifest.xmlに次の内容を追加
(activity/intent-filter内に追加)

<action android:name="android.intent.action.MAIN" /> <!-- この行は既にあるかも -->
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />

確認画面の表示

MainActivity.kt内のonCreate関数の適切な場所(例: 最後)に次の内容を追加

// Android 10以降かどうか確認
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    val roleManager = getSystemService(RoleManager::class.java)
    if (roleManager != null && !roleManager.isRoleHeld(RoleManager.ROLE_HOME)) {
    // ホームアプリとしてのロールをリクエスト
        val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_HOME)
        startActivityForResult(intent, REQUEST_CODE_ROLE_HOME)
    }
}
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