從Android N開始谷歌Launcher提供了shortcut功能,方便用戶能更快的使用應(yīng)用提供的功能服務(wù)(比如下圖),當(dāng)你每次支付的時(shí)候都要打開應(yīng)用,找到支付的按鈕,是不是這種方式更便捷呢。
這里就要詳細(xì)說說第三方應(yīng)用如何添加和管理自己的shortcut。 (推薦學(xué)習(xí):phpstorm)
Android對(duì)shortcut是有權(quán)限要求的,不是所有的Launcher都能展示應(yīng)用的shortcut,源碼里對(duì)權(quán)限是這么說明的
Only the default launcher can access the shortcut information.
只有被設(shè)置為默認(rèn)Launcher的桌面才有獲取shortcut的權(quán)限。如何讓非默認(rèn)桌面也獲取shortcut權(quán)限呢 也是有辦法的,后面再研究。
靜態(tài)shortcut
Android支持靜態(tài)shutcut和動(dòng)態(tài)shortcut,對(duì)于動(dòng)態(tài)的shortcut不需要在manifest里做任何的配置,對(duì)于靜態(tài)的只需要聲明一下meta-data即可
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
動(dòng)態(tài)的shortcut
framework提供了ShortcutManager來管理shortcut, 獲取ShortcutManager的方法如下
mShortcutManager = mContext.getSystemService(ShortcutManager.class);
shortcut的更新
如果用戶切換語言,shortcut該如何更新呢?可能你早就猜到了,ShortcutManager肯定提供了更新的接口,需要重新獲取一遍資源并set遍,最后調(diào)用下面的接口更新就好了。
public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) { ... }
shortcut功能為應(yīng)用提供了快捷的入口,應(yīng)用不但可以設(shè)置靜態(tài)的shortcut,也可以設(shè)置動(dòng)態(tài)的,也可以使能或者禁止shortcut,還可以根據(jù)用戶的使用行為,為不同的用戶提供不同的shortcut。
shortcut功能需要android系統(tǒng) sdk要在25以上,可惜的是國內(nèi)的手機(jī)廠商更新較慢,還沒有大量普及,不過我相信這兩年就會(huì)有大量的應(yīng)用提供這個(gè)功能