效果:
两种方式:静态和动态
静态:
1、xml目录下新建一个文件,比如命名为static_shortcuts.xml:
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/ic_search_black_24dp"
android:shortcutId="search"
android:shortcutDisabledMessage="@string/search"
android:shortcutLongLabel="@string/search"
android:shortcutShortLabel="@string/search"
tools:targetApi="n_mr1" >
<intent
android:action="android:intent.action.VIEW"
android:targetClass="com.xaehu.shortcut.activity.MainActivity"
android:targetPackage="com.xaehu.shortcut" />
<intent
android:action="android:intent.action.VIEW"
android:targetClass="com.xaehu.shortcut.activity.SearchActivity"
android:targetPackage="com.xaehu.shortcut" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:enabled="true"
android:icon="@drawable/ic_widgets_black_24dp"
android:shortcutId="all"
android:shortcutDisabledMessage="@string/all"
android:shortcutLongLabel="@string/all"
android:shortcutShortLabel="@string/all"
tools:targetApi="n_mr1" >
<intent
android:action="android:intent.action.VIEW"
android:targetClass="com.xaehu.shortcut.activity.MainActivity"
android:targetPackage="com.xaehu.xakan" />
<intent
android:action="android:intent.action.VIEW"
android:targetClass="com.xaehu.shortcut.activity.AllVideoActivity"
android:targetPackage="com.xaehu.xakan" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
2、AndroidManifest.xml里面启动的App那里添加一个<meta-data />:
<activity android:name=".activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden|locale|uiMode"
android:screenOrientation="portrait">
<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/static_shortcuts" />
</activity>
就可以了。
但是实际开发可能会遇到Intent需要带数据给你要打开的页面的,那静态的方法就解决不了了,用动态的方法:
动态:
addDynamicShortcuts方法可以添加一个快捷方式,同时保留静态声明的快捷方式,如果add的快捷方式id一样会覆盖上去。
setDynamicShortcuts方法可以完全替换掉原来的。
updateShortcuts可以更新已有的快捷方式
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(this);
if (dynamicShortcuts.size() == 1) {
ShortcutInfoCompat infoCompat = dynamicShortcuts.get(0);
infoCompat.getIntents()[1].putExtra(BaseConstant.Key_line, line);
ShortcutManagerCompat.updateShortcuts(this,dynamicShortcuts);
}else{
//点击返回走的Intent
Intent homeIntent = new Intent(this,MainActivity.class);
homeIntent.setAction(Intent.ACTION_VIEW);
//点击快捷方式走的Intent
Intent intent = new Intent(this,DetailActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(BaseConstant.Key_isStar, true);
intent.putExtra(BaseConstant.Key_line, line);
Intent[] intents = {homeIntent,intent};
ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(this,"star")
.setShortLabel(getString(R.string.star))
.setLongLabel(getString(R.string.star))
.setIcon(IconCompat.createWithResource(this,R.drawable.ic_stars_black_24dp))
.setIntents(intents)
.build();
//如果有多个快捷方式,可以用一个list装起来传给下面的第二个参数。
ShortcutManagerCompat.addDynamicShortcuts(this, Collections.singletonList(info));
}