android7.1新特性 App Shortcuts 圖標快捷啓動

前幾天看了下谷歌的發佈會,在介紹7.1的適合展示了圖標快捷啓動的功能,類似於蘋果的3d touch ,好酷炫,於是決定自己動手研究一波

在動手開發之前呢先將AS升級到最新版,SDK更新到API25,因爲只有API25才能使用這個功能。

首先介紹一下核心的幾個類,

ShortcutManager 該類是圖標信息管理者,主要負責添加,更新或去除圖標上的某個條目

2 Shortcutinfo 該類是圖標條目信息類,整個類代表一個條目,該類可以設置條目信息,包括圖標,標題,主體內容,隱藏內容

Shortcutinfo.Binder 該類用來創建一個Shortcut條目,返回Shortcutinfo對象

我們再來看看幾個XML屬性:

android:shortcutId   條目信息的ID

android:enabled     條目信息是否顯示,默認是顯示的

android:icon           條目信息的圖標

android:shortcutShortLabel  條目信息的標題(有正文的時候不會顯示)

android:shortcutLongLabel  條目信息的正文

android:shortcutDisabledMessage 當android:enabled  爲false的時候顯示的內容

intent 用戶點擊後跳轉的intent對象,強制要求有android:action 屬性


OK,瞭解完上面的屬性以後,我們開始動手實現下吧,


1,在AndroidMainfest.xml文件下,在主程序入口的<activity>標籤線新加入一個<meta-data>標籤,用來設置圖標條目的信息,其內容如下:

<meta-data android:name="android.app.shortcuts"
    android:resource="@xml/shortcuts" />
2 在res目錄下新建一個xml文件夾,在文件夾下新建一個shortcuts的xml文件,該文件用來靜態設置條目的信息
內容如下:
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="compose"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/compose_shortcut_short_label1"
        android:shortcutLongLabel="@string/compose_shortcut_long_label1"
        android:shortcutDisabledMessage="@string/compose_disabled_message1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.wenxi.myapplication"
            android:targetClass="com.example.wenxi.myapplication.MainActivity" />
        <!-- If your shortcut is associated with multiple intents, include them
             here. The last intent in the list is what the user sees when they
             launch this shortcut. -->
        <categories android:name="android.shortcut.conversation" />
    </shortcut>

    <!-- Specify more shortcuts here. -->
</shortcuts>

OK,簡單兩步,就完成了最簡單的demo,當然這還不夠,前面我說過,這是靜態的,要是需求裏面要動態更新條目信息呢,這樣我們就不能夠用xml去做了,我們需要用代碼實現上面的功能
首先,初始化ShortcutManager
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
//返回shortcutManager 對象
其次,我們需要通過Shortcutinfo.Binder去創建 Shortcutinfo 對象
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
該方法中傳入的參數是當前Activity的上下文和條目的ID
拿到 shortcut,對象以後,我們就可以設置條目信息了
.setShortLabel("Web site")//設置標題
.setLongLabel("Open"+String.valueOf(index)+"")//設置
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
//.setActivity(new ComponentName("com.example.wenxi.myapplication","com.example.wenxi.myapplication.MainActivity"))
.setIntent(new Intent(Intent.ACTION_VIEW,
        Uri.parse("https://www.baidu.com/")))//設置點擊跳轉的intent對象
.build();//創建
這裏需要注意的是,動態設置必須要設置intent對象,否則會報空指針異常,還有就是不知道是不是因爲bug,
setActivity並不起作用
定義一個list,將shortcut添加到list裏面
private List<ShortcutInfo> list=new ArrayList();
list.add(shortcut);

最後,通過ShortcutManager添加信息條目或者刪除條目:主要有下面三個方法:
setDynamicShortcuts(List) //將添加一個list
 updateShortcuts(List)   // 更新某一個List
removeDynamicShortcuts(List) //刪除某個list
removeAllDynamicShortcuts().//刪除全部信息
注:可以有多個list信息集合
本文采用shortcutManager.setDynamicShortcuts(list);
運行結果如圖:



源碼下載地址:http://download.csdn.net/detail/qq_25817651/9673658













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