在Android 7.1中,開發人員可以創建AppShortCut。如何使用適用於android 7.1應用程序的ShortcutManager API創建動態應用程序快捷方式?
我們可以通過兩種方式創建快捷方式:
- 使用的資源(XML)文件靜態的快捷方式。
- 使用
ShortcutManager
API的動態快捷方式。
那麼如何動態創建使用ShortcutManager
的快捷方式?
在Android 7.1中,開發人員可以創建AppShortCut。如何使用適用於android 7.1應用程序的ShortcutManager API創建動態應用程序快捷方式?
我們可以通過兩種方式創建快捷方式:
ShortcutManager
API的動態快捷方式。那麼如何動態創建使用ShortcutManager
的快捷方式?
使用ShortcutManager
,我們可以在下面的方式來創建應用程序動態應用程序快捷方式:
ShortcutManager shortcutManager;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
shortcut = new ShortcutInfo.Builder(this, "second_shortcut")
.setShortLabel(getString(R.string.str_shortcut_two))
.setLongLabel(getString(R.string.str_shortcut_two_desc))
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setIntent(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.google.co.in")))
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
}
}
字符串資源:
<string name="str_shortcut_two">Shortcut 2</string>
<string name="str_shortcut_two_desc">Shortcut using code</string>
開發者也可以使用ShortcutManager
執行不同任務的應用程序的快捷方式:
檢查Github的例子爲App Shortcut
檢查https://developer.android.com/preview/shortcuts.html和ShortcutManager獲得更多的信息。
M? API 25之後不可用嗎? –
我們可以使用ShortcutManager像這樣
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo webShortcut = new ShortcutInfo.Builder(this, "shortcut_web")
.setShortLabel("catinean.com")
.setLongLabel("Open catinean.com web site")
.setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic_shortcut))
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://catinean.com")))
.build();
shortcutManager.setDynamicShortcuts(Collections.singletonList(webShortcut));
我們可以使用ShortcutManager打開活動這樣
ShortcutInfo dynamicShortcut = new ShortcutInfo.Builder(this, "shortcut_dynamic")
.setShortLabel("Dynamic")
.setLongLabel("Open dynamic shortcut")
.setIcon(Icon.createWithResource(this, R.drawable.ic_dynamic_shortcut_2))
.setIntents(
new Intent[]{
new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
})
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(webShortcut, dynamicShortcut));
AppShortcuts簡單的項目意向行動:https://developer.android.com /samples/AppShortcuts/index.html – Benny