相比ios系統,Android的可定制性還是蠻高的,例如通知欄,ios的通知欄也是最近的幾個版本中才加了一些折疊的效果,一鍵清除等功能,但是通知欄的高度定制在Android系統中早已經司空見慣了,今天的主題不是通知欄,而是app的快捷方式,ios和android都可以創建快捷方式,但這里要向ios學習下了,在ios中默認為每一個app添加了一個 分享 的快捷方式,尤其是在3D Touch的加持下,快捷方式的可玩性更高
之所以叫快捷方式,是因為用戶可以在不打開app的情況下,長按app啟動圖標,快速打開指定的頁面,非常直接的一種方式,節省了一些不必要的操作,在Android中創建快捷方式有三種方式 靜態快捷方式,動態快捷方式,固定快捷方式
不幸的是,這幾種方式都需要在Android api 25 + ,只能呵呵了,不過還是值得操作一通的,畢竟功能還是很實用的,注意,雖然可以添加多個快捷方式,但是靜態+動態快捷方式最多只能在app啟動圖標上面顯示四個快捷方式
靜態快捷方式,需要在清單文件中聲明,注意,是在程序的入口Main里設置
<activity
android:name=".activity.MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTop"
android:theme="@style/SplashTheme">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!--創建靜態快捷方式-->
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
另外需要創建一個新的資源文件:
res/xml-v25/shortcuts.xml
。這個是要顯示的快捷方式布局
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/ic_vector_basic_info_24dp"
android:shortcutId="個人中心"
android:shortcutShortLabel="@string/basic_info">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.sxt.chat.activity.BasicInfoActivity"
android:targetPackage="com.sxt.chat" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
shortcuts節點下可以包含多個shortcut ,但是最多顯示4個,shortcut中必要的字段有兩個shortcutId 和 shortcutShortLabel
enabled:屬性可以控制該快捷方式是否顯示 。
icon:屬性是該快捷方式的drawable資源
shortcutId:屬性是該快捷方式的id,只能是字符串,不能是字符串的資源id。
shortcutShortLabel:屬性是該快捷方式的描述信息,只能是字符串的資源id。
shortcutLongLabel:屬性是描述快捷方式擴展信息,同樣也只能是字符串的資源id。如果有足夠的空間,啟動器會顯示此值而不是shortcutShortLabel
。如果可能,將快捷方式的“長描述”的長度限制為25個字符。
然后就是配置Intent信息,我們這里是要打開某一個activity,所以配置為具體的類名和包名
運行app看下效果
靜態快捷方式是只會創建一次,由于是固定在xml文件中,所以無法動態更新,只能通過版本更新apk的方式實現shortcut的更新
動態快捷方式相對來說比較靈活,可以隨時更新之前創建好的shortcut
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "動態創建的快捷方式")
.setShortLabel("動態創建的shortcut")
.setLongLabel("動態創建的shortcut-打開網頁")
.setIcon(Icon.createWithResource(this, R.drawable.ic_ar_photo_main_blue_24dp))
.setIntent(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://blog.csdn.net/sxt_zls")))
.build();
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
}
ShortcutInfo可以通過ShortcutInfo.Builder通過鏈式設置不同的屬性值,與靜態創建的方式屬性一致,這里的Intent可以設置為任意你想要的意圖,我這里設置的打開一個網頁,最后shortcutManager.setDynamicShortcuts實現快捷方式的創建,后續可以調用updateShortcuts方法來修改快捷方式
可以看到,長按app啟動圖標,出現了我們剛剛設置的快捷方式,點擊后打開了指定的網頁
相比靜態和動態創建的方式來說,固定快捷方式比較麻煩,因為通過固定的方式創建的shortcut會固定在手機主頁上面,所以需要用戶授權才能創建,當然,用戶可以選擇拒絕,拒絕后將無法創建該快捷方式,看下交互效果會更容易理解
下圖是通過動態的方式打開activity,然后申請添加快捷方式,選擇自動添加或者拖動圖標到主頁即可添加成功
最后看下代碼的實現,與動態注冊類似,只不過需要設置創建之后的回調
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager.isRequestPinShortcutSupported()) {
Intent intent=new Intent(Intent.ACTION_VIEW,null, this, VR360Activity.class);
ShortcutInfo pinShortcutInfo =
new ShortcutInfo.Builder(this, "固定的快捷方式")
.setShortLabel("固定的快捷方式")
.setIcon(Icon.createWithResource(this, R.drawable.ic_ar_photo_main_blue_24dp))
.setIntent(intent)
.build();
Intent pinnedShortcutCallbackIntent =
shortcutManager.createShortcutResultIntent(pinShortcutInfo);
//配置意圖,以便應用程序的廣播接收器回調成功的廣播。
PendingIntent successCallback = PendingIntent.getBroadcast(this, 0,
pinnedShortcutCallbackIntent, 0);
shortcutManager.requestPinShortcut(pinShortcutInfo,
successCallback.getIntentSender());
}
}
需要注意的是,并不是所有8.0以上的設備都支持固定模式的快捷方式,這里需要通過isRequestPinShortcutSupported()方法來進行判斷目標設備是否支持該功能