Android App Shortcuts

簡介

Android 7.1容許您定義應用程序中特定操做的快捷方式。這些快捷鍵能夠顯示桌面,例如Nexus和Pixel設備。快捷鍵可以讓您的用戶在應用程序中快速啓動常見或推薦的任務。
每一個快捷鍵引用一個或多個意圖,每一個意圖在用戶選擇快捷方式時在應用程序中啓動特定操做。能夠表達爲快捷方式的操做示例包括:

在跳轉頁面時將用戶導航到特定位置。
在通信應用程式中傳送訊息給朋友。
在媒體應用中播放電視節目的下一集。
在遊戲應用程序中加載最後一個保存點。html

App Shortcuts,一次最多可爲您的應用發佈4個快捷方式,當超過4個時,只顯示最新四個,動態添加會拋Max number of dynamic shortcuts exceeded。可是,用戶能夠將應用的快捷方式複製到啓動器上,從而建立固定的快捷方式。用戶能夠建立和訪問無限數量的固定快捷方式,以觸發應用中的操做。java

更多介紹:developer.android.com/preview/sho…android

效果預覽


說明:須要長按桌面圖標,而後就能夠定義進入本身想要的頁面了


說明:能夠長按拖出建立一個固定的快捷方式

使用方法

xml實現

一、AndroidManifest.xml
啓動頁,添加meta-data標籤git

<activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
    <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
</activity>複製代碼

二、res/xml/shortcuts.xmlgithub

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut android:enabled="true" android:icon="@drawable/ic_content_copy_24dp" android:shortcutDisabledMessage="@string/shortcut_disabled_message1" android:shortcutId="shortcutId1" android:shortcutLongLabel="@string/shortcut_long_label1" android:shortcutShortLabel="@string/shortcut_short_label1">
        <intent android:action="action1" android:targetClass="com.wuxiaolong.designsupportlibrarysample.AppShortcutsActivity" android:targetPackage="com.wuxiaolong.designsupportlibrarysample"/>
    </shortcut>
    <shortcut android:enabled="true" android:icon="@drawable/ic_share_24dp" android:shortcutDisabledMessage="@string/shortcut_disabled_message1" android:shortcutId="shortcutId2" android:shortcutLongLabel="@string/shortcut_long_label2" android:shortcutShortLabel="@string/shortcut_short_label2">
        <intent android:action="action2" android:targetClass="com.wuxiaolong.designsupportlibrarysample.BottomNavigationActivity" android:targetPackage="com.wuxiaolong.designsupportlibrarysample"/>
    </shortcut>

</shortcuts>複製代碼

說明:android:shortcutLongLabel和android:shortcutShortLabel,顯示文本,默認顯示long,當long很長,就顯示short;android:targetClass跳轉的頁面;android:targetPackage包名web

代碼實現

添加App Shortcuts微信

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut = new ShortcutInfo.Builder(mActivity, "shortcutId3")
        .setShortLabel("Web site")
        .setLongLabel("Open the web site")
        .setIcon(Icon.createWithResource(mActivity, R.drawable.ic_link_24dp))
        .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://wuxiaolong.me/")))
        .build();
try {
    shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
} catch (Exception e) {
    e.printStackTrace();
}複製代碼

刪除App Shortcutsapp

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
List<ShortcutInfo> shortcutInfoList = shortcutManager.getDynamicShortcuts();//能夠作個list管理App Shortcuts,這裏略
List<String> list = new ArrayList<>();
list.add("shortcutId3");
try {
    shortcutManager.removeDynamicShortcuts(list);
} catch (Exception e) {
    e.printStackTrace();
}複製代碼

隱藏App Shortcutsui

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
 List<String> list = new ArrayList<>();
 list.add("shortcutId3");
 try {
     shortcutManager.disableShortcuts(list);
 } catch (Exception e) {
     e.printStackTrace();
 }複製代碼

這樣就實現了App Shortcuts效果了spa

源碼

github.com/WuXiaolong/…

感謝

官方文檔
pcevikogullari/AndroidShortcuts

最後

App Shortcuts只能在Android 7.1手機纔有的效果,很炫很便捷,也不知道國內手機何時能看到App Shortcuts真容。

微信公衆號

個人公衆號:吳小龍同窗,歡迎微信掃一掃關注。

相關文章
相關標籤/搜索