App Shortcuts快捷方式

App Shortcuts簡介

Android 7.1 - App ShortcutsAndroid 7.1 新功能之一就是 App Shortcuts(應(yīng)用快捷方式) ,該功能與 iPhone 上的 3D Touch 功能相似,通過長按應(yīng)用圖標,可彈出應(yīng)用快捷方式,點擊可以直接跳轉(zhuǎn)到相應(yīng)的界面。目前最多支持 5 個快捷方式,可以 getMaxShortcutCountPerActivity() 查看 Launcher 最多支持幾個快捷方式,不同的是 Android 支持通過拖拽將快捷方式固定到桌面。但是,實應(yīng)用快捷方式還是有很多缺陷的:

  • 只能在 Google 的 Nexus 及 Pixel 設(shè)備上使用
  • 系統(tǒng)必須是 Android 7.1 及以上(API Level >= 25)
  • 已經(jīng)被用戶固定到桌面的快捷方式必須得到兼容性處理,因為你基本上失去了對其控制,除了升級時禁用

App Shortcuts注冊方式

每一個應(yīng)用目前最多可以有5個shortcuts(靜態(tài) + 動態(tài)),擁有靜態(tài)注冊/動態(tài)注冊方式

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

  • 靜態(tài)的: 在xml中定義, 適用于一些通用的動作
  • 動態(tài)的: 由ShortcutManager發(fā)布, 可以根據(jù)用戶的行為或者偏好添加, 可以動態(tài)更新

靜態(tài)注冊方式

靜態(tài)的Shortcuts是寫在xml中的, 直到下一次應(yīng)用升級, 不能被改變.
添加靜態(tài)shortcuts只需兩步:
  1. 在應(yīng)用的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>
    
  2. 在res/xml/目錄下創(chuàng)建shortcuts.xml文件, 里面包含靜態(tài)的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>
    

動態(tài)Shortcuts使用

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

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

比如添加一個動態(tài)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));

文章參考:http://www.cnblogs.com/mengdd/p/5996665.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容