Android7.1新特性shortcut文档以外的东西

app shortcut官方文档 shortcut是android7.1推出的新特性,仿ios的3d touch功能,在android平台上表现为长按弹出快捷方式,7.1系统上谷歌自家app均支持了该功能,例如系统app YouTube等。html

简介

app shortcut分为static shortcut和Dynamic short,能够类比android广播分动态注册静态注册来理解,具体看文档便可理解,因项目使用的功能静态注册方式可实现,这里只谈谈静态注册android

文档中没提的坑

  • 模拟器上会出现莫名其妙的问题,建议这功能直接真机测试ios

  • 文档intent中有个targetPackage配置项,初一看觉得是packname,实际是applicationID配置。插个知识点在eclipse项目结构时packname和applicationID是同一律念或者说两者值一致,但android studio项目结构时,两者是不一样的applicationID应用程序的惟一标识而packname仅仅指项目的包名并无惟一性。 所以debug release包该参数须要动态配置 1.在build.gradle文件中定义一个常量,分debug和release赋不一样值,再shortcuts.xml中调用,结果 行不通 2.google到的方案https://github.com/Zellius/android-shortcut-gradle-plugin/blob/master/README.md 大概用了一个第三方库,不在shortcuts.xml中对targetPackage直接赋值,在代码中实现 测试可行 3.用隐试intent方案,绕过targetpackage配置, 结果行不通 4.固然讲debug release包分开编译调用不一样代码方式确定是能解决这个问题的。但有点拿大炮打蚊子感受 故放弃该方案git

  • 还有个小case,快捷方式指向购物车页面,若是在该页面back,按照文档上写是直接推出app,其实能够实现从back到指定页面,相似咱们app中back回的首页,这是比较合乎常理的,实现只需在对应intent上加上back会页面的intent。github

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
   <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.mainactivity" />
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.ComposeActivity" />
    <categories android:name="android.shortcut.conversation" />
  </shortcut>
  <!-- Specify more shortcuts here. -->
</shortcuts>
    

复制代码

小结

固然把app shortcut使用的更加人性化,还有不少小细节能够处理的更加完美,例如app运行时跟非运行时back页面的区别处理。 快捷方式的更新处理,等等。。总之,基础的功能实现容易,作得更好仍是须要下翻功夫的bash

2017年3月23更新

关于targetPackage自动配置提供一个新方案,直接gradle写个脚本替换shortcuts.xml文件中targetPackage的值app

def replaceInShortcuts(variant, fromString, toString) {
    def flavor = variant.productFlavors.get(0)
    def buildType = variant.buildType
    def shortcutsFile = "$buildDir/intermediates/res/merged/${flavor.name}/${buildType.name}/xml/shortcuts.xml"
    def file = new File(shortcutsFile);
    def updatedContent = file.getText('UTF-8').replaceAll(fromString, toString)
    file.write(updatedContent, 'UTF-8')
}  


 output.processResources.doFirst {
   replaceInShortcuts(variant, '\\{PCK\\}', variant.applicationId)
 }

复制代码

注:本文做者在私人帐号发表过,这次是在公司帐号发布。eclipse

相关文章
相关标签/搜索