Android 7.1.1 之實現 3D Touch

來源:互聯網
上載者:User

標籤:api   lan   action   tty   自訂   pack   比較   article   .sh   

轉載請註明出處:http://blog.csdn.net/yyh352091626/article/details/68962736

    • Shortcut概念
    • 具體實現
      • BuildConfig 配置
      • 靜態配置
      • 動態配置
        • 建立
        • 刪除或禁用
        • 更新
    • 返回棧問題

Shortcut概念

Shortcut 是Android-25(Android 7.1)新增的一項類似iOS的 3D Touch 功能的捷徑群組件,但是有著不同的表現形式,因為Android在硬體上不支援觸摸壓力感應,所以表現形式為長按,而iOS須用力長按。

首先,來個

   

在 Launcher 或 應用程式列表 裡面,長按應用表徵圖,彈出一個捷徑列表, 並且,可以把單個捷徑拖動出來作為一個案頭表徵圖,拖出來的表徵圖會隨著清除應用資料或卸載應用而消失,須重新建立。

具體實現BuildConfig 配置

在主module下,修改 build.grade,使其使用 android-25 的 API 編譯,當然,未下載的,就需要開啟Android SDK Manager下載一下。

android {    compileSdkVersion 25     buildToolsVersion "25.0.0" // 或以上    defaultConfig {        targetSdkVersion 25    }}

靜態配置

類似BroadCastReceiver,Shortcut註冊也分為靜態註冊和動態註冊,首先介紹靜態註冊,動態註冊後面繼續~~

  1. 在 res/xml 檔案夾底下建立一個xml,舉個栗子:shortcut.xml

    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><shortcut    android:enabled="true"    android:icon="@mipmap/ic_bar_detail_write"    android:shortcutDisabledMessage="@string/shortcut_publish"    android:shortcutId="publish"    android:shortcutLongLabel="@string/shortcut_publish"    android:shortcutShortLabel="@string/shortcut_publish">    <intent        android:action="android.intent.action.VIEW"        android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity"        android:targetPackage="com.yanshi.writing" />    <categories android:name="android.shortcut.conversation" /></shortcut><shortcut    android:enabled="true"    android:icon="@mipmap/logo"    android:shortcutDisabledMessage="@string/shortcut_write"    android:shortcutId="write"    android:shortcutLongLabel="@string/shortcut_write"    android:shortcutShortLabel="@string/shortcut_write">    <intent        android:action="android.intent.action.VIEW"        android:targetClass="com.yanshi.writing.ui.write.WriteActivity"        android:targetPackage="com.yanshi.writing" />    <categories android:name="android.shortcut.conversation" /></shortcut></shortcuts>

    1、enabled:表示當前捷徑是否可使用
    2、 icon: 捷徑表徵圖
    3、 shortcutDisabledMessage: 捷徑不可使用時顯示的名字
    4、 shortcutId:捷徑標識
    5、 shortcutLongLabel:長按標彈出來列表框中每個快捷名
    6、 shortcutShortLabel:快捷是可以單獨顯示在案頭上的,顯示名為shortcutShortLabel
    7、 targetClass:點擊捷徑進入的Activity
    8、categories 預設寫死即可

  2. 資訊清單檔註冊
    在 AndroidMainfest.xml 的預設啟動頁裡添加 meta-data 標籤配置

    <activity        android:name=".ui.MainActivity"        android:configChanges="orientation|screenSize|keyboardHidden"        android:screenOrientation="portrait"        android:theme="@style/AppTheme.NoneTranslucent">        <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/shortcut" />    </activity>
  3. 完畢! 可以到案頭查看效果了~~
動態配置

動態建立增加了菜單配置的靈活性,比如可以從服務端拉取捷徑列表,再進行展示。具體配置方法如下:

建立

在需要註冊的地方添加如下代碼:

/**  * 動態建立  */ public void register() {     ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);     List<ShortcutInfo> infos = new ArrayList<>();     // 按下返回按鈕跳轉的activity     Intent intent1 = new Intent(this, MainActivity.class);     intent1.setAction(Intent.ACTION_VIEW);     // 目標activity     Intent intent2 = new Intent(this, PublishPostActivity.class);     intent2.setAction("com.yuyh.xxx.BACK");     Intent[] intents = new Intent[2];     intents[0] = intent1;     intents[1] = intent2;     ShortcutInfo info = new ShortcutInfo.Builder(this, "publish-2")             .setShortLabel("動態建立-發布文章")             .setLongLabel("動態建立-發布文章")             .setIcon(Icon.createWithResource(this, R.mipmap.ic_bar_detail_write))             .setIntents(intents)             .build();     infos.add(info);     mShortcutManager.setDynamicShortcuts(infos); }

重新運行app,再次長按,效果如下:

刪除或禁用

動態刪除可以刪除動態配置的捷徑。

/** * 動態刪除 */public void delete() {    ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);    /********* 移除彈出列表表徵圖 **********/    // 所有動態建立表徵圖    List<ShortcutInfo> infos1 = mShortcutManager.getDynamicShortcuts();    List<String> ids1 = new ArrayList<>();    for (ShortcutInfo info : infos1 ) {        ids1.add(info.getId());    }    // 禁用所有的捷徑    mShortcutManager.disableShortcuts(ids1, "已禁用");    mShortcutManager.removeDynamicShortcuts(ids1);    /********* 移除拖出來的案頭快捷表徵圖 **********/    // 放在案頭的表徵圖    List<ShortcutInfo> infos2 = mShortcutManager.getPinnedShortcuts();    List<String> ids2 = new ArrayList<>();    for (ShortcutInfo info : infos2 ) {        ids2.add(info.getId());    }    mShortcutManager.disableShortcuts(ids2, "已禁用");    mShortcutManager.removeAllDynamicShortcuts();}

代碼比較簡單,就不多做敘述了。 須注意一下 getPinnedShortcuts 方法與 getDynamicShortcuts 方法的區別! 禁用後的效果,表徵圖變成灰色:

更新

捷徑的唯一性,由前面提到的 shortcutId 這個標識符決定,所以更新捷徑與建立捷徑一樣, shortcutId 如果相同, 則會覆蓋之前建立的捷徑!

返回棧問題

當通過捷徑開啟時,現有的Activity都會被銷毀,然後重新建立一個Activity棧。因為清單方式設定的快速鍵的Intent不能自訂Intent的Flag,其預設的Flag是 FLAG_ACTIVITY_NEW_TASK 和 FLAG_ACTIVITY_CLEAR_TASK。

通過動態註冊的方式,可發現,我們可以配置返回目標activity。當然,靜態配置也可以實現,修改shortcut標籤:

<shortcut     android:enabled="true"     android:icon="@mipmap/ic_bar_detail_write"     android:shortcutDisabledMessage="@string/shortcut_publish"     android:shortcutId="publish"     android:shortcutLongLabel="@string/shortcut_publish"     android:shortcutShortLabel="@string/shortcut_publish">     <!-- 返回目標activity -->     <intent         android:action="com.yuyh.xxx.BACK"         android:targetClass="com.yanshi.writing.ui.MainActivity"         android:targetPackage="com.yanshi.writing" />     <!-- 目標activity -->     <intent         android:action="android.intent.action.VIEW"         android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity"         android:targetPackage="com.yanshi.writing" />     <categories android:name="android.shortcut.conversation" /> </shortcut>

感謝閱讀!

Android 7.1.1 之實現 3D Touch

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.