Android 8.0+(二) 應用快捷方式 Shortcut

    相比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()方法來進行判斷目標設備是否支持該功能

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章