Android 7.1初体验之应用快捷键

本文详细介绍了Android 7.1中新增的App快捷键功能,包括清单创建和动态创建两种方式。清单快捷键与应用版本相关,动态快捷键更灵活,允许根据用户需求设置。每个应用最多可设置5个快捷键。动态创建快捷键时,可通过ShortcutManager API进行发布、更新和删除。快捷键的Intent和显示顺序也进行了深入探讨,强调了动态和清单快捷键的差异。
摘要由CSDN通过智能技术生成

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.

  1. 快捷键打开应用程序时,展现的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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值