Android 桌面的二級菜單Shortcut 實(shí)現(xiàn)

寫在前面的話

本文在http://blog.csdn.net/qibin0506/article/details/52878690上進(jìn)行補(bǔ)充。
Google官方文檔:https://developer.android.google.cn/guide/topics/ui/shortcuts.html
如果您的應(yīng)用的目標(biāo)是Android 7.1(API級別25)或更高,則可以在應(yīng)用中定義 快捷方式以適應(yīng)特定操作??旖莘绞娇勺屇挠脩粼趹?yīng)用內(nèi)快速啟動常用或推薦的任務(wù)。顯示如圖

image

靜態(tài)使用 Shortcut

1.res/xml/ 下新建一個(gè)xml 文件,此處取名為mandroid.xml
eq:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true" //該二級菜單是否可用
        android:icon="@mipmap/xiansuo" //顯示的圖標(biāo)
        android:shortcutDisabledMessage="@string/quxian"http://不可用時(shí)顯示的文字
        android:shortcutId="settings"http://唯一id 
        android:shortcutLongLabel="@string/settings_long_name" //長名字
        android:shortcutShortLabel="@string/settings_short_name">//短名字

        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.gray.quxian.TestActivity"
            android:targetPackage="com.gray.quxian" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

外部標(biāo)簽為<shortcuts>,內(nèi)部標(biāo)簽為<shortcut>.如果有多個(gè)菜單的話,就寫平級的<shortcut>標(biāo)簽。
intent 的 targetPackage要和manifest 的包名一致,targetClass的值為目標(biāo)頁面值,<categories>的標(biāo)簽內(nèi)的 name 值是固定的。

2.配置清單文件
在程序的主入口下配置<meta-data>并且name的值固定,resource的值為之前的創(chuàng)建的xml文件

<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>

動態(tài)創(chuàng)建 Shortcut

shortcut的代碼創(chuàng)建方式要比靜態(tài)創(chuàng)建方式復(fù)雜些,但是方便我們更新迭代,所以要更加常用些。下面我們就來學(xué)習(xí)如何代碼動態(tài)設(shè)置shortcut

1.初始化shortcut

/**
 * 初始化shortcut
 */
 //初始化shortManager
ShortcutManager mSystemService;
mSystemService = getSystemService(ShortcutManager.class);
List<String> mTitle = new ArrayList<>();

private void initShortsCut() {
    List<ShortcutInfo> dynamicShortcuts = new ArrayList<>();
    
    //動態(tài)設(shè)置及添加shortcut 其中g(shù)etMaxShortCutCountPerActivity的值是5所以mTitle的值不要少于5個(gè),或者一個(gè)一個(gè)的初始化
    
    for (int i = 0; i < mSystemService.getMaxShortcutCountPerActivity(); i++) {
        Intent intent = new Intent(this, OtherActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i)//設(shè)置id
                .setShortLabel(mTitle.get(i))//設(shè)置短標(biāo)題
                .setLongLabel("功能:" + mTitle.get(i))//設(shè)置長標(biāo)題
                .setIcon(Icon.createWithResource(this, R.mipmap.xiansuo))//設(shè)置圖標(biāo)
                .setIntent(intent)//設(shè)置intent
                .build();
        dynamicShortcuts.add(info);//將新建好的shortcut添加到集合
    }
    mSystemService.setDynamicShortcuts(dynamicShortcuts);//設(shè)置動態(tài)shortcut
}

2.更新shortcut
要通過id去更新shortcut

private void updataShortCut(int id) {
    Intent intent = new Intent(this, OtherActivity.class);//目標(biāo)頁面
    intent.setAction(Intent.ACTION_VIEW);
    ShortcutInfo info = new ShortcutInfo.Builder(this, id)
            .setShortLabel("已修改")//設(shè)置短標(biāo)題
            .setLongLabel("功能:已修改修改")//設(shè)置長標(biāo)題
            .setIcon(Icon.createWithResource(this, R.mipmap.fengji))//設(shè)置圖標(biāo)
            .setIntent(intent)
            .build();
    mSystemService.updateShortcuts(Arrays.asList(info));//更新shortcut
}

3.刪除shortcut
如果app的功能點(diǎn)被迭代了刪除了,那么該功能的shortcut的點(diǎn)擊會出現(xiàn)崩潰的,我們需要讓這個(gè)shortcut失效。同更新shortcut,都是通過id 進(jìn)行操作。

/**
 * 刪除已有功能后該二級菜單失效
 */
private void deleteShortCut(int index) {
    List<ShortcutInfo> infos = mSystemService.getPinnedShortcuts();
    //遍歷出該id的shortcut
    for (ShortcutInfo info : infos) {
        if (info.getId().equals("id" + index)) {
            mSystemService.disableShortcuts(Arrays.asList(info.getId()), "暫無改功能");
        }
    }
    //動態(tài)刪除該功能
    mSystemService.removeDynamicShortcuts(Arrays.asList("id" + index));
}

代碼中刪除shortcut之后,android home界面中的shortcut 就回被置灰,點(diǎn)擊彈toastdisableShortcuts 的內(nèi)容。

總結(jié)

shortcut 是 Android 7.0以后 google 新增的功能,體驗(yàn)類很棒,除了增加了應(yīng)用功能的快速入口,沒有其他影響,所以我覺得只要 app 適配了 7.0,8.0 ,這個(gè)功能就可以加上,提高用戶的體驗(yàn)。本篇教學(xué)只是簡單的教學(xué),更深入的學(xué)習(xí)可以看文章開頭的 google 文檔,會有一些提高的。

更多內(nèi)容,歡迎關(guān)注我的微信公眾號 MAndroid ,定期發(fā)福利哦~~


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

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,725評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,502評論 19 139
  • 第一次看見你我雖有點(diǎn)懵,但再看見你是我對你怦然心動。初見頃心,再見動心,忘記不了你的面容,愛上你我不會錯(cuò)。 ...
    狂妄閱讀 233評論 0 1
  • 當(dāng)高考成績閃現(xiàn)在屏幕上時(shí),我的內(nèi)心里沒有預(yù)料中的激動與興奮,有的只是平淡與冷靜。反而感覺到一種解脫與釋放。...
    陳陌沫閱讀 590評論 8 6
  • 1. 斯里蘭卡、馬爾代夫的工程建設(shè),很多交給了中國的公司??苽惼路比A地段見到在建香格里拉二期,查了下是中國港灣在建...
    張振亞童鞋閱讀 284評論 0 0

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