Android 8.0 (Oreo) 前後ショートカット作成方法の違い - Android Studio
Android 8 (O) API26 からショートカットの作成方法が変わったのでメモ
ダイナミックショートカットは、アプリアイコンをうにゅっと押し続けて4つくらいまで表示されるショートカットの事です(iPhoneでいう3D Touch のクイックアクション)
requestPinShortcut の第2引き数はショートカット作成後のイベントらしいです、エミュレータでは requestPinShortcut 処理後にOSからショートカット作成確認のダイアログが出てましたので、作成が完了したかどうかなんかが受け取れるのかも(未確認)
また、(操作の話ですが)ダイナミックショートカットを長押しして、フツーのショートカットとしてコピーする事が出来ます
String shortcutName = "Shortcut Name";
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, "com.example.myApplication"); // 呼び出すActivity
shortcutIntent.putExtra("key1", "value1"); // 渡す情報()
shortcutIntent.putExtra("key2", "value2");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8 O API26 以降
Icon icon = Icon.createWithResource(getApplicationContext(), R.mipmap.ic_shortcut);
ShortcutInfo shortcut = new ShortcutInfo.Builder(getApplicationContext(), shortcutName)
.setShortLabel(shortcutName)
.setLongLabel(shortcutName)
.setIcon(icon)
.setIntent(shortcutIntent)
.build();
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
shortcutManager.requestPinShortcut(shortcut, null); // フツーのショートカット
// shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut)); // ダイナミックショートカット
} else {
// Android 7 Nougat API25 以前
Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_shortcut);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
this.sendBroadcast(intent);
}
ダイナミックショートカットは、アプリアイコンをうにゅっと押し続けて4つくらいまで表示されるショートカットの事です(iPhoneでいう3D Touch のクイックアクション)
requestPinShortcut の第2引き数はショートカット作成後のイベントらしいです、エミュレータでは requestPinShortcut 処理後にOSからショートカット作成確認のダイアログが出てましたので、作成が完了したかどうかなんかが受け取れるのかも(未確認)
// 一覧取得 ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); Listダイナミックショートカットを操作できるのは該当アプリケーションのみですshortcutList = shortcutManager.getDynamicShortcuts(); for (int i = 0; i < shortcutList.size(); i++) { shortcutList.get(i); } // 削除 ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.removeDynamicShortcuts(Arrays.asList("ショートカット名"));
また、(操作の話ですが)ダイナミックショートカットを長押しして、フツーのショートカットとしてコピーする事が出来ます

コメント