Android7.1 的3d touch------Shortcuts

靜態(tài)添加Shortcuts功能

先上效果圖:

Paste_Image.png

1.在AndroidManifest.xml文件的主activity下面配置shortcuts,如下圖:

Paste_Image.png

主要代碼:

 <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts" />

2.在res文件下創(chuàng)建xml文件夾,創(chuàng)建showrtcuts.xml

Paste_Image.png

然后編寫相關的代碼(我這里寫了三個快捷):

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="compose1"
        android:enabled="true"
        android:icon="@drawable/ic_menu_joke"
        android:shortcutShortLabel="@string/joke"
        android:shortcutLongLabel="@string/joke"
        android:shortcutDisabledMessage="@string/joke">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.chenxinwen.baiduapi"
            android:targetClass="com.chenxinwen.baiduapi.ui.joke.JokeActivity" />
        <!-- If your shortcut is associated with multiple intents, include them
             here. The last intent in the list is what the user sees when they
             launch this shortcut. -->
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <!-- Specify more shortcuts here. -->

    <shortcut
        android:shortcutId="compose2"
        android:enabled="true"
        android:icon="@drawable/ic_menu_gallery"
        android:shortcutShortLabel="@string/gallery"
        android:shortcutLongLabel="@string/gallery"
        android:shortcutDisabledMessage="@string/gallery">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.chenxinwen.baiduapi"
            android:targetClass="com.chenxinwen.baiduapi.ui.gallery.GalleryActivity" />
        <!-- If your shortcut is associated with multiple intents, include them
             here. The last intent in the list is what the user sees when they
             launch this shortcut. -->
        <categories android:name="android.shortcut.conversation" />
    </shortcut>

    <shortcut
        android:shortcutId="compose3"
        android:enabled="true"
        android:icon="@drawable/ic_menu_slideshow"
        android:shortcutShortLabel="@string/province_select"
        android:shortcutLongLabel="@string/province_select"
        android:shortcutDisabledMessage="@string/province_select">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.chenxinwen.baiduapi"
            android:targetClass="com.chenxinwen.baiduapi.ui.province_select.ProvinceActivity" />
        <!-- If your shortcut is associated with multiple intents, include them
             here. The last intent in the list is what the user sees when they
             launch this shortcut. -->
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

3.在build.gradle中修改compileSdkVersion和buildToolsVersion,至少要是25以上才可以.

Paste_Image.png

Demo下載,github地址

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

相關閱讀更多精彩內容

友情鏈接更多精彩內容