1.给应用页面注册快捷方式(静态):
打开res/values目录下的string.xml,添加下述字符串:
<string name="first_short">first</string>
<string name="first_long">扫一扫</string>
<string name="second_short">second</string>
<string name="second_long">付钱</string>
<string name="third_short">third</string>
<string name="third_long">收钱</string>
<string name="four_short">four</string>
<string name="four_long">更多设置</string>
接着在res目录下创建名为xml的文件夹,并在该文件夹下创建shortcuts.xml(名字任意),保存以下内容:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@mipmap/scan_round"
android:shortcutId="first"
android:shortcutLongLabel="@string/first_long"
android:shortcutShortLabel="@string/first_short">
<!-- targetClass 指定了点击该项菜单后要打开那个活动页面-->
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.example.test.Login"
android:targetPackage="com.example.test" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
<!--有多少段就复制多少次(最多四个,官方说的五个)-->
</shortcuts>
其中:shortcutId:快捷方式的编号(可随意编写,但不能重复,重复不显示)
enable:是否启用快捷方式,true为启用,false为禁用(不显示)
icon:快捷方式左侧的图标
shortcutShortLabel:快捷方式的短标签(10字以内)
shortcutsLongLabel:快捷方式的长标签(25字以内),展示不下就显示短标签
intent:同Intent
action:有两个1.android.intent.action.VIEW,显示页面2.com.example.shortcut.back(项目包),点击返回后的界面
targetClass:打开的类名
targetPackage:完整的包名
接着打开AndroidManifest.xml,在MainActivity的结点中添加元数据配置:
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
完整的MainActivity如下:
<activity android:name=".MainActivity">
<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/shortcuts" />
</activity>
接下来,遇到的坑:
①点击快捷菜单列表显示应用程序未安装:一般是targetClass打错了,检查targetClass和targetPackage
②点击快捷菜单列表无反应:检查activity目录,要放根目录根目录才行,深层目录无法跳转,尚不知道为什么,还可能是targetClass打错了,检查检查就好
shortcutsDemo(github):https://github.com/huonie/ShortcutsDemo.git