ShortCuts的使用

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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容