Android 8 Oreo がリリースされて少し経ちました。
新機能をさっと眺めていたところホームショートカットの作成が、今までの方法ではできなくなると見かけました。
Android Oではcom.android.launcher.action.INSTALL_SHORTCUTが無効化された。かわりにrequestPinShortcutを使う。
そこで実際に確かめてみました。
emulator だけでの確認ですが、Oreo からは Broadcast による登録は不可能、requestPinShortcut() からは以下の UI から登録できました。
Android 8 以降
ShortcutManager#requestPinShortcut() を使います。以前に必要であった AndroidManifest.xml への permission を追加する必要はありません。
val manager = getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager
val info = ShortcutInfo.Builder(this, "shortcut-id")
.setShortLabel("label")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher_round))
.setIntent(intent)
.build()
manager.requestPinShortcut(info, null)
これだけですが、API >= 26 が必要です。
Android 7.1 以前
こちらも比較のために乗せておきます。
AndroidManifest.xml へ
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
を追加した上で、
Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT")
...
sendBroadcast(intent)
Broadcast するだけです。Android 8 でこれを実行しても、残念ながら登録されません。エラーも表示されないため、知らないと意味がわからないかと思います。
Android 8 でホームショートカットを作成する皆様の参考になれば幸いです。