轉載請註明出處:http://blog.csdn.net/yyh352091626/article/details/68962736
Shortcut概念
Shortcut 是Android-25(Android 7.1)新增的一項類似iOS的 3D
Touch
功能的快捷方式組件,但是有着不同的表現形式,因爲Android在硬件上不支持觸摸壓力感應,所以表現形式爲長按,而iOS須用力長按。
首先,來個效果圖
在 Launcher 或 應用程序列表 裏面,長按應用圖標,彈出一個快捷方式列表, 並且,可以把單個快捷方式拖動出來作爲一個桌面圖標,拖出來的圖標會隨着清除應用數據或卸載應用而消失,須重新創建。
具體實現
BuildConfig 配置
在主module下,修改 build.grade,使其使用 android-25 的 API 編譯,當然,未下載的,就需要打開Android SDK Manager下載一下。
android {
compileSdkVersion 25
buildToolsVersion "25.0.0" // 或以上
defaultConfig {
targetSdkVersion 25
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
靜態配置
類似BroadCastReceiver,Shortcut註冊也分爲靜態註冊和動態註冊,首先介紹靜態註冊,動態註冊後面繼續~~
-
在
res/xml
文件夾底下創建一個xml,舉個栗子:shortcut.xml<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:enabled="true" android:icon="@mipmap/ic_bar_detail_write" android:shortcutDisabledMessage="@string/shortcut_publish" android:shortcutId="publish" android:shortcutLongLabel="@string/shortcut_publish" android:shortcutShortLabel="@string/shortcut_publish"> <intent android:action="android.intent.action.VIEW" android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity" android:targetPackage="com.yanshi.writing" /> <categories android:name="android.shortcut.conversation" /> </shortcut> <shortcut android:enabled="true" android:icon="@mipmap/logo" android:shortcutDisabledMessage="@string/shortcut_write" android:shortcutId="write" android:shortcutLongLabel="@string/shortcut_write" android:shortcutShortLabel="@string/shortcut_write"> <intent android:action="android.intent.action.VIEW" android:targetClass="com.yanshi.writing.ui.write.WriteActivity" android:targetPackage="com.yanshi.writing" /> <categories android:name="android.shortcut.conversation" /> </shortcut> </shortcuts>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
1、enabled:表示當前快捷方式是否可使用
2、 icon: 快捷方式圖標
3、 shortcutDisabledMessage: 快捷方式不可使用時顯示的名字
4、 shortcutId:快捷方式標識
5、 shortcutLongLabel:長按下圖標彈出來列表框中每個快捷名
6、 shortcutShortLabel:快捷是可以單獨顯示在桌面上的,顯示名爲shortcutShortLabel
7、 targetClass:點擊快捷方式進入的Activity
8、categories 默認寫死即可 -
清單文件註冊
在 AndroidMainfest.xml 的默認啓動頁裏添加meta-data
標籤配置<activity android:name=".ui.MainActivity" android:configChanges="orientation|screenSize|keyboardHidden" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoneTranslucent"> <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/shortcut" /> </activity>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 完畢! 可以到桌面查看效果了~~
動態配置
動態創建增加了菜單配置的靈活性,比如可以從服務端拉取快捷方式列表,再進行展示。具體配置方法如下:
創建
在需要註冊的地方添加如下代碼:
/**
* 動態創建
*/
public void register() {
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
List<ShortcutInfo> infos = new ArrayList<>();
// 按下返回按鈕跳轉的activity
Intent intent1 = new Intent(this, MainActivity.class);
intent1.setAction(Intent.ACTION_VIEW);
// 目標activity
Intent intent2 = new Intent(this, PublishPostActivity.class);
intent2.setAction("com.yuyh.xxx.BACK");
Intent[] intents = new Intent[2];
intents[0] = intent1;
intents[1] = intent2;
ShortcutInfo info = new ShortcutInfo.Builder(this, "publish-2")
.setShortLabel("動態創建-發佈帖子")
.setLongLabel("動態創建-發佈帖子")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_bar_detail_write))
.setIntents(intents)
.build();
infos.add(info);
mShortcutManager.setDynamicShortcuts(infos);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
重新運行app,再次長按,效果如下:
刪除或禁用
動態刪除可以刪除動態配置的快捷方式。
/**
* 動態刪除
*/
public void delete() {
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
/********* 移除彈出列表圖標 **********/
// 所有動態創建圖標
List<ShortcutInfo> infos1 = mShortcutManager.getDynamicShortcuts();
List<String> ids1 = new ArrayList<>();
for (ShortcutInfo info : infos1 ) {
ids1.add(info.getId());
}
// 禁用所有的快捷方式
mShortcutManager.disableShortcuts(ids1, "已禁用");
mShortcutManager.removeDynamicShortcuts(ids1);
/********* 移除拖出來的桌面快捷圖標 **********/
// 放在桌面的圖標
List<ShortcutInfo> infos2 = mShortcutManager.getPinnedShortcuts();
List<String> ids2 = new ArrayList<>();
for (ShortcutInfo info : infos2 ) {
ids2.add(info.getId());
}
mShortcutManager.disableShortcuts(ids2, "已禁用");
mShortcutManager.removeAllDynamicShortcuts();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
代碼比較簡單,就不多做敘述了。 須注意一下 getPinnedShortcuts
方法與 getDynamicShortcuts
方法的區別!
禁用後的效果如圖所示,圖標變成灰色:
更新
快捷方式的唯一性,由前面提到的 shortcutId
這個標識符決定,所以更新快捷方式與創建快捷方式一樣, shortcutId
如果相同,
則會覆蓋之前創建的快捷方式!
返回棧問題
當通過快捷方式打開時,現有的Activity都會被銷燬,然後重新創建一個Activity棧。因爲清單方式設置的快捷鍵的Intent不能自定義Intent的Flag,其默認的Flag是 FLAG_ACTIVITY_NEW_TASK
和 FLAG_ACTIVITY_CLEAR_TASK
。
通過動態註冊的方式,可發現,我們可以配置返回目標activity。當然,靜態配置也可以實現,修改shortcut標籤:
<shortcut
android:enabled="true"
android:icon="@mipmap/ic_bar_detail_write"
android:shortcutDisabledMessage="@string/shortcut_publish"
android:shortcutId="publish"
android:shortcutLongLabel="@string/shortcut_publish"
android:shortcutShortLabel="@string/shortcut_publish">
<!-- 返回目標activity -->
<intent
android:action="com.yuyh.xxx.BACK"
android:targetClass="com.yanshi.writing.ui.MainActivity"
android:targetPackage="com.yanshi.writing" />
<!-- 目標activity -->
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity"
android:targetPackage="com.yanshi.writing" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
感謝閱讀!