苹果在iPhone 6s和iPhone 6s Plus开始新增了3D Touch,这个功能褒贬不一,但我以为仍是特别好用的,特别是在微信上付款、加好友、扫二维码,或是在支付宝上付款等等场景。好用的真的是不要不要的,下面是苹果3D Touch的截图:html
这么好用的功能安卓能不模仿吗?呸!能不实现吗?java
从安卓7.1(25)开始,系统新增了Shortcut来实现相似苹果3D Touch的功能,实现效果以下:android
苹果的3D Touch是经过检测手指按压力度来判断是否弹出,安卓硬件没办法统一,因此统一采用长按的方式来实现。web
实现呢,一共有三种方式,安卓官方是这样来描述的:浏览器
我就用官方直译来翻译下凑合理解吧:服务器
快捷方式可帮助用户快速访问应用程序的各个部分,从而为用户提供特定类型的内容。微信
使用快捷方式交付内容的方式取决于您的用例以及快捷方式的上下文是应用程序驱动仍是用户驱动。尽管静态快捷方式的上下文不会更改,而动态快捷方式的上下文会不断更改,可是在两种状况下,上下文都是由您的应用程序驱动的。若是用户选择本身但愿您的应用向其交付内容的方式(例如固定快捷方式),则上下文由用户定义。如下方案演示了每种快捷方式类型的一些用例:app
一、在AndroidManifest.xml中找见你的启动项Activity,在其中添加一个元素,该元素引用定义了应用程序快捷方式的资源文件:ide
<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>
二、上面的shortcuts文件详情以下,其中定义了基本参数和意图:性能
<?xml version="1.0" encoding="utf-8"?> <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:enabled="true" android:icon="@drawable/one" android:shortcutDisabledMessage="@string/static_disabled_message" android:shortcutId="staticId" android:shortcutLongLabel="@string/static_shortcut_long_label" android:shortcutShortLabel="@string/static_shortcut_short_label"> <categories android:name="android.shortcut.conversation" /> <intent android:action="android.intent.action.VIEW" android:targetClass="com.zj.shortcut.Main2Activity" android:targetPackage="com.zj.shortcut" /> </shortcut> </shortcuts>
三、上面的文件中有不少的属性,下面来一一讲解一下:
android:shortcutId
和 android:shortcutShortLabel
是必填值,其余都是可选的。 android:shortcutId
字符串文字,表示对象对其执行操做时的快捷方式。android:shortcutShortLabel
简短的描述,描述了快捷方式的用途。尽量将快捷方式的“简短描述”的长度限制为10个字符,否则在手机上显示效果可能不太好。android:shortcutLongLabel
描述该快捷方式用途的扩展短语。若是有足够的空间,启动器将显示此值而不是上面的简短的描述。若是可能,将快捷方式的“详细描述”的长度限制为25个字符。android:shortcutDisabledMessage
当用户尝试启动禁用的快捷方式时,在受支持的启动器中显示的消息。该消息应向用户说明为何如今禁用了快捷方式。若是android:enabled
为,则此属性的值无效true
。android:enabled
肯定用户是否能够与受支持的启动器中的快捷方式进行交互。默认值android:enabled
是true
。若是将其设置为false
,则还应该设置一个android:shortcutDisabledMessage
,以说明为何禁用了快捷方式。若是您不须要提供这样的消息,则最简单的方法是从XML文件中彻底删除快捷方式。android:icon
启动器在向用户显示快捷方式时使用 的位图 或 自适应图标。该值能够是图像的路径,也能够是包含该图像的资源文件。尽量使用自适应图标来提升性能和一致性。四、上面的字符串都定义在了res->value->string.xml中,下面是代码:
<string name="static_disabled_message">测试信息</string> <string name="static_shortcut_long_label">测试长标题</string> <string name="static_shortcut_short_label">测试短标题</string>
五、下面来看一下静态展现安卓的“3D Touch”:
一、动态快捷键提供了指向应用程序中特定于上下文的特定操做的连接。这些操做可能会在您的应用使用之间发生变化,甚至在应用运行时也会发生变化。动态快捷方式的选择包括呼叫特定人员,导航至特定位置以及从用户的最后保存点加载游戏等等。
这是ShortcutManagerAPI的地址:
https://developer.android.google.cn/reference/android/content/pm/ShortcutManager.html
setDynamicShortcuts()
从新定义动态快捷键的完整列表,或者使用 addDynamicShortcuts()
以增长动态快捷键的现有列表。 updateShortcuts()
方法。 removeDynamicShortcuts()
,或使用 删除全部动态快捷方式 removeAllDynamicShortcuts()
。二、下面是动态添加快捷方式的代码(注意要加版本注解):
@TargetApi(Build.VERSION_CODES.N_MR1) @RequiresApi(Build.VERSION_CODES.M) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val shortcutManager = getSystemService<ShortcutManager>(ShortcutManager::class.java) val shortcut = ShortcutInfo.Builder(this, "id1") .setShortLabel("Website") .setLongLabel("Open the website") .setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_background)) .setIntent( Intent( Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/")) ) .build() shortcutManager!!.dynamicShortcuts = listOf(shortcut) }
三、下面是添加完以后运行界面样式:
在Android 8.0(API级别26)及更高版本上,能够建立固定的快捷方式。与静态和动态快捷方式不一样,固定的快捷方式在支持的启动器中显示为单独的图标。图1显示了这两种快捷方式之间的区别。
一、使用 isRequestPinShortcutSupported()
验证应用程序内设备的默认启动支持快捷键。
二、ShortcutInfo
根据快捷方式是否已存在,以两种方式之一建立对象:
ShortcutInfo
仅包含现有快捷方式ID的对象。系统会自动查找并固定与快捷方式相关的全部其余信息ShortcutInfo
对象,其中包含新快捷方式 的ID,意图和简短标签。 三、经过调用将快捷方式固定到设备的启动器 requestPinShortcut()
。在此过程当中,能够传入一个 PendingIntent
对象,该对象仅在成功固定快捷方式后才通知应用。
注意:若是用户不容许将快捷方式固定在启动器上,则您的应用不会收到回调。固定快捷方式后,可使用 updateShortcuts()
方法更新其内容 。
四、下面是建立固定快捷方式的代码:
@SuppressLint("NewApi") private fun initShortcut() { val shortcutManager = getSystemService(ShortcutManager::class.java) if (shortcutManager!!.isRequestPinShortcutSupported) { val pinShortcutInfo = ShortcutInfo.Builder(this, "id1") .setShortLabel("Website") .setLongLabel("Open the website") .setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_background)) .setIntent( Intent( Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/")) ) .build() val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo) val successCallback = PendingIntent.getBroadcast(this, /* request code */ 0, pinnedShortcutCallbackIntent, /* flags */ 0) shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.intentSender) } }
五、运行结果以下:
本文参照了官网Shortcut的介绍及使用,本文涉及全部代码都可联系我获取。
欢迎你们关注个人我的公众号,会按期发布安卓、Java学习及搞笑文章。