美文网首页
ShortCuts的使用

ShortCuts的使用

作者: 小贱嘎嘎 | 来源:发表于2017-06-27 17:43 被阅读0次

ShortCuts是什么?

先看一张图


ShortCuts示例

ShortCuts就是那浮出来的四个条目,当点击每个条目时,系统会寻找应用里相应的Activity并打开,方便直接导航用户至app中重要的页面。但ShortCuts只能在Api 25(Android 7.1)及以上版本的手机中才能使用。

ShortCuts的类别

  • ****静态快捷键****
    静态快捷键在资源文件中定义,因此他会被打包至apk中,所以只能通过更新整个app的方式才能更新快捷键。

  • ****动态快捷键****
    动态快捷键是在app运行时使用ShortManager API动态发布的。因此我们能在运行的时候发布,删除,更新动态快捷键。

ShortCuts的用法

  • ****静态快捷键****
    1, 在AndroidManifest.xml文件中找到app启动页面(intent-filter属性配置如下所示)
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    2,在上一步找到的Activity节点中添加<meta-data>元素,这个<meta-data>元素指定了快捷键需要加载的资源文件(资源文件一般放在res/xml目录下),如下所示:
    <activity android:name="Main">
    <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>
    3,在res/xml目录下新建shortcuts.xml文件,编辑快捷键配置
    <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:targetClass="xxx.xxx.xxx.FirstActivity"
    .../>
    <intent
    android:action="android.intent.action.VIEW"
    android:targetPackage="com.example.myapplication"
    android:targetClass="com.example.myapplication.ComposeActivity" />
    </shortcut>
    </shortcuts>

相关文章

网友评论

      本文标题:ShortCuts的使用

      本文链接:https://www.haomeiwen.com/subject/blvecxtx.html