3DTouch技术用于IOS系统以后,受到了果粉的一致推捧。Android用户的福音来了,App Shortcuts完美的展现了3DTouch,个人感觉比3DTouch更为强大,其不仅仅有3DTouch的效果,同时能为该快捷键设置桌面图标。
介绍
使用新的快捷键功能将用户从启动器直接带到应用程序中的关键操作。用户只需长按应用程序的启动器图标即可显示应用程序的快捷键,然后点按快捷键即可跳转到相关操作,比如建发送新短信,播放视频,继续游戏等操作。
Google官方提供了两种添加快捷键的键,一种是添加到APK中的资源文件来清单创建应用程序的快捷键(即为清单键),另外一种是运行时动态添加。对于常用的操作,官方推荐的是使用清单键添加,动态键显得更加的灵活,可以根据用户的喜好来设置,显得更为人性化。
每个应用程序并不能无限的设定快捷键,现官方规定每个应用程序最多设置5个快捷键。
清单创建
清单快捷键是应用程序内部的通用连接,其有效性与应用程序的版本相关联。若版本发生的变更,原清单快捷键也相应的失效。
下面我们来看看如何创建快捷键:
1.设定当前应用程序的入口
这地方不用说,都知道为了设置程序的入口,luncher这个activity通过获取应用程序信息来加载应用程序,显示给用户,其中就是通过一个应用程序中的AndroidManifest.xml中的声明来识别的。
Android.intent.action.MAIN 与 android.intent.category.LAUNCHER 决定应用程序在luncher中的显示键,其中,android.intent.action.MAIN决定应用程序最先启动的Activity,而android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里。
2.添加,引用定义应用程序快捷键的资源文件
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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>
</application>
在主入口Activity中,添加上,是为了指定应用程序可以引用定义应用程序快捷键的资源文件。其实际意义应该是将主入口的Activity引用变为资源文件中定义的Activity.
快捷键打开应用程序时,展现的Activity中引用,打开应用程序时指定跳转的Activity.
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <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> <activity android:name=".ActivityA"> </activity> *** </application>
3.在res/xml目录下创建shortcuts.xml资源文件。该资源文件指定了,此快键键相对应的操作,以及快捷键相关的UI。
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="a"
android:enabled="true"
android:icon="@mipmap/ic_1"
android:shortcutShortLabel="@string/compose_shortcut_short_a"
android:shortcutLongLabel="@string/compose_shortcut_long_a"
android:shortcutDisabledMessage="@string/compose_shortcut_disabled_a">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.tea.appshortcutsdemo"
android:targetClass="com.example.tea.appshortcutsdemo.ActivityA" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
***
</shortcuts >
相关API:
- android:shortcutId=”a”:设定快捷的ID
- android:enabled=”true”: 设定该快捷键是否可用
- android:icon=”@mipmap/ic_1”:快捷键的Logo
- android:shortcutShortLabel=”XX”:快捷键的短标签
- android:shortcutLongLabel=”XX”:快捷键的长标签
- android:shortcutDi