寫在前面的話
本文在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ù)。顯示如圖

靜態(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)擊彈toast 為 disableShortcuts 的內(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
