Android 7.1 App Shortcuts使用

来源:http://www.cnblogs.com/mengdd/archive/2016/10/25/5996665.html
-Advertisement-
Play Games

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
微信公眾號


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在這裡只粘貼部分代碼 在第一課中,只有View滑動完畢,才觸發動畫效果,令滑塊移動,在第二課中,將實現滑塊與View同步運行。 SecondActivity.java 下麵詳細介紹ViewPager.OnPageChangeListener監聽器的三個重寫方法: 當從手指按下,到頁面滑動停止的過程: ...
  • 有一種方法可以阻止父層的View截獲touch事件,就是調用 getParent().requestDisallowInterceptTouchEvent(true);方法。一旦底層View收到touch的 action後調用這個方法那麼父層View就不會再調用onInterceptTouchEve ...
  • 在學校開課學習了android的一些簡單的UI組件,佈局,四大組件學習了2個,數據存儲及網路通信,都是一些簡單的概念,入門而已。許多東西需要自己去學習。 學習一下 Android開發環境的搭建,兩種方式開發:一種是Eclipse,另一種是Android Studio。 Eclipse 一、下載and ...
  • 1. JSONObject對象的optXXX和getXXX的區別? getInt("key") 取值 不存在 或者類型不對 報錯optInt("key") 取值 不存在 返回預設值 getDouble("key") 取值 不存在 或者類型不對 報錯optDouble("key",0) 取值 不存在 ...
  • 在Android 4.4系統中,外置存儲卡(SD卡)被稱為二級外部存儲設備(secondary storage),應用程式已無法往外置存儲卡(SD卡)寫入數據,並且WRITE_EXTERNAL_STORAGE只為設備上的主要外部存儲(primary storage)授予寫許可權,對於其他外部存儲,其上 ...
  • 滿網都是微信小程式,技術dog們不關註都不行了。先別忙著去學怎麼開發小程式,先糾正一下你對微信小程式的三觀吧~~~~ 小程式目前被炒得沸沸揚揚,無數媒體和企業藉機獲取閱讀流量。 這再次證明一點,微信想讓什麼火,真的就能讓什麼火。 先列出8個多數人都搞錯的問題: 以上8個是很多人憑直覺得出的結論,但真 ...
  • 關於Retrofit+OkHttp的強大這裡就不多說了,還沒瞭解的同學可以自行去百度。這篇文章主要講如何利用Retrofit+OkHttp來實現一個較為簡單的緩存策略:即有網環境下我們請求數據時,如果沒有緩存或者緩存過期了,就去伺服器拿數據,並且將新緩存保存下來,如果有緩存而且沒有過期,則直接使用緩 ...
  • 想要瞭解Android新版本的的新特性,從頭開始吧,這是Android3.0新加入的widget,以前也接觸過,但是沒有好好的研究過,今天寫了一個小程式,研究一下ViewPager。 這個程式是支持左右滑動的View,核心是ViewPager。講解都在註釋中進行。 代碼如下: MainActivit ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...