Android 7.1 App Shortcuts使用


Android 7.1 App Shortcuts使用

Android 7.1已經發了預覽版, 這里是API Overview: API overview.
其中App Shortcuts是新提供的一種快捷訪問方式, 形式為長按應用圖標出現的長條.

app shortcuts

圖來自: Exploring Android Nougat 7.1 App Shortcuts

點擊快捷方式可以訪問應用功能, 並且這種快捷方式也可以被拖拽到桌面單獨放置.

App Shortcuts 是什么

其中App Shortcuts是指在桌面長按app圖標而出現的快捷方式, 可以為你的app的關鍵功能添加更快速的入口而不用先打開app.

Android 7 Nougat app shortcuts

點擊快捷方式可以訪問應用功能, 並且這種快捷方式也可以被拖拽到桌面單獨放置, 變成單獨的桌面快捷方式(pinned shortcuts).

有兩種shortcuts:

  • 靜態的: 在xml中定義, 適用於一些通用的動作.
  • 動態的: 由ShortcutManager發布, 可以根據用戶的行為或者偏好添加, 可以動態更新.

每一個應用目前最多可以有5個shortcuts(靜態 + 動態).

運行條件:
應用添加App Shortcuts是Android 7.1(API 25)的API, 所以只能在Android 7.1的設備上顯示, 同時需要launcher支持, 比如Pixel launcher(Pixel設備的默認launcher), Now launcher(Nexus設備上的launcher)現在就支持, 其他launcher也可以提供支持.

靜態Shortcuts使用

靜態的Shortcuts是寫在xml中的, 直到下一次應用升級, 不能被改變.
要添加靜態shortcuts只需兩步:
首先, 在應用的Manifest中啟動Activity上添加<meta-data>:

    <activity android:name=".MainActivity">
        <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.xml文件, 里面包含靜態的shortcuts:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_check_circle_black_24dp"
        android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"
        android:shortcutId="static"
        android:shortcutLongLabel="@string/static_shortcut_long_label_1"
        android:shortcutShortLabel="@string/static_shortcut_short_label_1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"
            android:targetPackage="com.ddmeng.hellonougat" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_android_black_24dp"
        android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"
        android:shortcutId="static_2"
        android:shortcutLongLabel="@string/static_shortcut_long_label_2"
        android:shortcutShortLabel="@string/static_shortcut_short_label_2">
        <intent
            android:action="android.intent.action.MAIN"
            android:targetClass="com.ddmeng.hellonougat.MainActivity"
            android:targetPackage="com.ddmeng.hellonougat" />
        <intent
            android:action="com.ddmeng.hellonougat.action.STATIC_SHORTCUT_2"
            android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"
            android:targetPackage="com.ddmeng.hellonougat" />
    </shortcut>
</shortcuts>

這就好了, 這個文件添加了兩個shortcuts, 點擊都將打開指定的Activity, 本例子中是StaticShortcutActivity.

用多個Intent構建back stack

上面這個文件里添加了兩個靜態的shortcuts, 第一個關聯了一個Activity, 點擊shortcut將直接打開這個Activity, 回退的時候回到桌面.

如果你想要的效果是點擊back鍵回到應用里的某個界面, 那么可以利用多個intents來構建back stack, 比如在第二個shortcut里面, 點擊shortcut還是打開目標Activity, 這個指定目標Activity的Intent放在最后, 但是回退會返回到MainActivity, 即之前的那個Intent.

動態Shortcuts使用

動態的shortcuts可以在用戶使用app的過程中構建, 更新, 或者刪除.
使用ShortcutManager可以對動態shortcuts完成下面幾種操作:

  • Publish發布: setDynamicShortcuts(), addDynamicShortcuts(List);
  • Update更新: updateShortcuts(List);
  • Remove刪除: removeDynamicShortcuts(List), removeAllDynamicShortcuts().

比如添加一個動態shortcut:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
    .setShortLabel("Web site")
    .setLongLabel("Open the web site")
    .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
    .setIntent(new Intent(Intent.ACTION_VIEW,
                   Uri.parse("https://www.mysite.example.com/")))
    .build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

點擊這個shortcut會發出一個打開網頁的Intent, 讓你選擇瀏覽器, 從而打開網址.

多個Intent構建back stack

動態的shortcut仍然可以用多個Intent來指定一個back stack, 那么打開目標Activity之后就可以返回到應用中的指定界面而不是回到launcher:

ShortcutInfo dynamicShortcut2 = new ShortcutInfo.Builder(this, "shortcut_dynamic")
        .setShortLabel("Dynamic Shortcut")
        .setLongLabel("Open Dynamic shortcut 2")
        .setIcon(Icon.createWithResource(this, R.drawable.ic_favorite_border_black_24dp))
        .setIntents(
                // this dynamic shortcut set up a back stack using Intents, when pressing back, will go to MainActivity
                // the last Intent is what the shortcut really opened
                new Intent[]{
                        new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                        new Intent(DynamicShortcutActivity.ACTION_OPEN_DYNAMIC)
                        // intent's action must be set
                })
        .build();

和靜態一樣, 最后一個Intent對應的是shortcut打開的界面DynamicShortcutActivity, 前面的都是用來構建back stack, 即back退回到MainActivity.
注意這里的Intent必須指定Action, 否則會拋出異常.

Shortcuts的個數限制

Shortcuts的總數不能超過5個, 即靜態和動態shortcuts加起來總數最多是五個.
當我們嘗試添加第六個shortcut時, 應用會拋出異常: java.lang.IllegalArgumentException: Max number of dynamic shortcuts exceeded.

雖然總數限制是5個, 但是當我正好有5個(2個靜態 + 3個動態)的時候, 長按只顯示了4個shortcuts.
如圖:

app shortcuts

本文完整代碼見: Demo地址: HelloNougat.

Shortcuts的次序

當我們有多個Shortcuts之后, 默認它們是按照添加順序排列的, 即按照添加順序rank遞增.

可以通過setRank()來改變長按時它們顯示的排序:

@TargetApi(25)
private void updateDynamicShortcuts() {
    ShortcutInfo webShortcut = new ShortcutInfo.Builder(MainActivity.this, "shortcut_blog")
            .setRank(1)
            .build();

    ShortcutInfo dynamicShortcut = new ShortcutInfo.Builder(MainActivity.this, "shortcut_dynamic")
            .setRank(0)
            .build();
    // the rank value can not be set to negative, otherwise will throw
    // java.lang.IllegalArgumentException: Rank cannot be negative or bigger than MAX_RANK

    // the static shortcuts have the rank 0, so they will always be closest to launcher icon

    shortcutManager.updateShortcuts(Arrays.asList(webShortcut, dynamicShortcut));
}

這樣更改之后, 原先排在最遠端的shortcut_dynamic被移到了第三個, shortcut_blog被移到了它的后面.

setRank()不接受負值, 會拋出異常.

我們只能改變動態shortcuts的排序, 靜態的shortcuts等級為0, 它們是按照xml中寫定的先后順序排的, 所以:
靜態的shortcuts永遠離應用icon最近, 動態shortcuts在其之上排序, rank越大的離應用icon越遠.
如果沒有指定rank, 則按生成的順序遞增.

參考

App Shortcuts的官方文檔: App Shortcuts
Exploring Android Nougat 7.1 App Shortcuts

Demo地址: HelloNougat.
近期考慮加入更多Android 7 Nougat特性sample.

最后, 歡迎關注微信公眾號: 聖騎士Wind
微信公眾號


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM