一、靜態方式
1.1、在res/xml/目錄下創建一個新的xml文件,這里我們命名為:shortcuts.xml。
代碼:
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="static1"
android:enabled="false"
android:icon="@drawable/launch"
android:shortcutDisabledMessage="@string/nav_btn_login"
android:shortcutLongLabel="@string/nav_btn_login"
android:shortcutShortLabel="@string/nav_btn_login">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.secondui.activity.TestActivity"
android:targetPackage="com.myProject" />
<categories android:name="android.shortcut.conversation"/>
</shortcut>
<shortcut
android:shortcutId="static2"
android:enabled="true"
android:icon="@drawable/launch"
android:shortcutDisabledMessage="@string/nav_btn_login"
android:shortcutLongLabel="@string/nav_btn_login"
android:shortcutShortLabel="@string/nav_btn_login">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.secondui.activity.LoginActivity"
android:targetPackage="com.myProject" />
<categories android:name="android.shortcut.conversation"/>
</shortcut>
</shortcuts>
代碼說明:
1、shortcutId 一個唯一的id,不能重復。
2、enabled 表示這個shortcut是否可用。不可用時,就不會顯示這個快捷方式。
3、shortcutShortLabel 這里是配置的短名稱。如果長名稱顯示不下, 就顯示短名稱。這里必須使用@string的方式,不能直接寫字符串。
4、shortcutLongLabel 這里是配置的長名稱。launcher會優先選擇長名稱顯示。這里必須使用@string的方式,不能直接寫字符串。
5、shortcutDisabledMessage 這個配置是在我們選擇一個不可用的shortcut時給用戶的一個提示。沒有意義,因為不可用時就不顯示這個快捷方式。這里必須使用@string的方式,不能直接寫字符串。
6、intent 這里表示我們點擊shortcut時要做什么。targetPackage是你的應用的包名;targetClass是我們要跳轉的目標類;這里要注意的是android:action一定要配置, 否則會崩潰。
7、categories 這個東西目前位置官方只給提供了android.shortcut.conversation。
1.2、在AndroidManifest.xml中啟動的activity中添加meta-data。
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
特別說明:只有在具有如下配置的activity中才能添加meta-data來使用快捷方式。
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
二、動態方式
2.1、主要的類ShortcutManager,其方法如下。
動態發布: setDynamicShortcuts(), addDynamicShortcuts(List);
動態更新: updateShortcuts(List);
動態刪除: removeDynamicShortcuts(List), removeAllDynamicShortcuts();
示例代碼:
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void qiTest(){
ShortcutManager shortcutManager=getSystemService(ShortcutManager.class);
//使用Dynamic Shortcuts
List<ShortcutInfo> shortcutInfoList=new ArrayList<>();
//掃一掃
Intent intent = new Intent(this, TestActivity.class);
intent.setAction(Intent.ACTION_VIEW);
ShortcutInfo info = new ShortcutInfo.Builder(this, TestActivity.class.getName())
.setShortLabel("掃一掃")
.setLongLabel("掃一掃")
.setIcon(Icon.createWithResource(this,R.drawable.launch))
.setIntent(intent)
.build();
shortcutInfoList.add(info);
//登錄
Intent intent2 = new Intent(this, LoginActivity.class);
intent2.setAction(Intent.ACTION_VIEW);
ShortcutInfo info2 = new ShortcutInfo.Builder(this, LoginActivity.class.getName())
.setShortLabel("登錄了")
.setLongLabel("來登錄")
.setIcon(Icon.createWithResource(this,R.drawable.launch))
.setIntents(new Intent[]{new Intent(this, StartActivity.class).setAction(Intent.ACTION_VIEW),intent2})
.build();
shortcutInfoList.add(info2);
shortcutManager.setDynamicShortcuts(shortcutInfoList);
//移除某Pinning Shortcuts
for (ShortcutInfo shortcutInfo: shortcutManager.getPinnedShortcuts()) {
if (shortcutInfo.getId().equals(TestActivity.class.getName()))shortcutManager.disableShortcuts(Arrays.asList(shortcutInfo.getId()),"已移除!");
}
//移除某Dynamic Shortcuts
shortcutManager.removeDynamicShortcuts(Arrays.asList(TestActivity.class.getName()));
}
特別說明:1、setIntents可以設置多個intent,從而實現back stack的效果,數組最后一個對象是目標類。只設置一個intent,返回后直接回退到手機桌面。
2、Pinning Shortcuts的操作只有用戶有權限,開發者不可操作,因此這里的移除的效果是快捷方式的圖標會被置灰,同時可以給用戶一個友好的提示語。
3、disableShortcuts方法被執行時不僅僅移除了Pinning Shortcuts而且會移除Dynamic Shortcuts,所以不需要再調用removeDynamicShortcuts方法。