Android 8.0+ (Oreo) で ホーム画面にアイコンを

Android 8.0 では、アプリのショートカットが次のように変更されています。

com.android.launcher.action.INSTALL_SHORTCUT ブロードキャストは、プライベートで暗黙的なブロードキャストになったため、アプリに影響を与えることはなくなりました。代わりに、ShortcutManager クラスの requestPinShortcut() メソッドを使ってアプリのショートカットを作成する必要があります。

ACTION_CREATE_SHORTCUT インテントによって、ShortcutManager クラスを使用して管理するアプリ ショートカットを作成できるようになりました。このインテントでは、ShortcutManager とやり取りをしない以前のランチャーのショートカットも作成できます。これまで、このインテントでは以前のランチャーのショートカットしか作成できませんでした。

Extension Function で。コピペ用。


inline fun Context.createShortcutHomeScreen() {
  if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
    val shortcutInfo = ShortcutInfoCompat.Builder(this, "abc123")
        .setIntent(
            Intent(this, MainActivity::class.java).apply {
              action = Intent.ACTION_MAIN // for Oreo
            }
        )
        .setShortLabel("テストです")
        .setIcon(IconCompat.createWithResource(this, R.drawable.ic_favorite_black))
        .build()
    ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null)
  } else {
    Timber.d("Shortcut is not supported by your launcher")
  }
}