Android学习——Shortcut

App Shortcut功能

最近手机在升级Android 7.1之后,长按某些APP图标就会弹出菜单,类似于IOS的3D touch功能,如下图:

è¿éåå¾çæè¿°

看了系统更新的文档才知道该功能叫做App Shortcut,目前只有少部分的应用支持这个功能,之后随着Android版本的更新,将会有大批APP适配该功能。

实现App Shortcuts有两种形式:

  • 动态形式:在运行时,通过ShortcutManager API来进行注册。通过这种方式,你可以在运行时,动态的发布,更新和删除Shortcut。
  • 静态形式:在APK中包含一个资源文件来描述Shortcut。这种注册方法将导致:如果你要更新Shortcut,你必须更新整个应用程序。

目前,每个应用最多可以注册5个Shortcuts,无论是动态形式还是静态形式。

通过动态形式注册的Shortcut,通常是特定的与用户使用上下文相关的一些动作。这些动作在用户的使用过程中,可能会发生变化。

ShortcutManager提供了API来动态管理Shortcut,包括:

  • 新建:方法setDynamicShortcuts() 可以添加或替换所有的shortcut;方法addDynamicShortcuts() 来添加新的shortcut到列表中,超过最大个数会报异常
  • 更新:方法updateShortcuts(List shortcutInfoList) 更新已有的动态快捷方式;
  • 删除:方法removeDynamicShortcuts(List shortcutIds) 根据动态快捷方式的ID,删除已有的动态快捷方式;方法removeAllDynamicShortcuts() 删除掉app中所有的动态快捷方式;
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
    .setShortLabel("Web site")
    .setLongLabel("Open the web site")
    .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
    .setIntent(new Intent(Intent.ACTION_VIEW,
                   Uri.parse("https://www.mysite.example.com/")))
    .build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

静态形式

静态Shortcut应当提供应用程序中比较通用的一些动作,例如:发送短信,设置闹钟等等。

开发者通过下面的方式来设置静态Shortcuts:

App Shortcuts是在Launcher上显示在应用程序的入口上的,因此需要设置在action为“android.intent.action.MAIN”,category为“ android.intent.category.LAUNCHER”的Activity上。通过添加一个 <meta-data> 子元素来并指定定义Shortcuts资源文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.short1">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SettingsActivity"></activity>
        <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>

</manifest>

在res/xml/shortcuts.xml这个资源文件中,添加一个 根元素,根元素中包含若干个 子元素,每个 描述了一个Shortcut,其中包含:icon,description labels以及启动应用的Intent。

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="settings"
        android:enabled="true" //该二级菜单是否可用
        android:icon="@drawable/ic_launcher_background" //显示的图标
        android:shortcutShortLabel="@string/settings_short_name" //短名字
        android:shortcutLongLabel="@string/settings_long_name" //长名字
        android:shortcutDisabledMessage="@string/settings_disable_msg"> //不可用时显示的文字

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.short1"
            android:targetClass="com.example.short1.SettingsActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>
</shortcuts>

外部标签为<shortcuts>,内部标签为<shortcut>.如果有多个菜单的话,就写平级的<shortcut>标签。
intent 的 targetPackage要和manifest 的包名一致,targetClass的值为目标页面值,<categories>的标签内的 name 值是固定的。

Shortcuts的简单作用

每个Shortcut可以关联一个或多个intents,每个intent启动一个指定的action; 官方给出了几个可以作为shortcut的例子,比如:

  • 在地图类app中,指导用户到特定的位置;
  • 在社交类app中,发送消息给一个朋友;
  • 在媒体类app中,播放视频的下一片段;
  • 在游戏类app中,下载最后保存的要点;

APP Shortcut功能很实用,能在自己的APP是适配该功能,用户体验将会进一步提高。

 

参考博客:https://blog.csdn.net/he_xiaochong/article/details/73011584

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
深度学习中有许多常用的技巧和快捷方式。以下是一些常见的深度学习shortcut: 1. 数据预处理:对数据进行适当的清洗、归一化、标准化和缩放,以提高训练效果和模型的稳定性。 2. 特征选择:通过特征选择技术,从原始数据中选择最相关和最有用的特征,以减少维度和降低计算复杂度。 3. 数据增强:通过对训练数据进行随机变换和扩充,如旋转、翻转、平移等,增加训练样本的多样性,提高模型的泛化能力。 4. 小批量训练:将大规模数据集分成多个小批量,每次使用一个小批量进行训练,以提高训练速度和模型的收敛速度。 5. 学习率调整:根据训练过程中的损失和准确率,动态调整学习率,以避免训练过程中出现震荡或停滞。 6. 正则化:通过添加正则化项(如L1正则化、L2正则化)来约束模型的复杂度,防止过拟合。 7. 提前停止:监控验证集的性能指标,在性能不再提升时提前终止训练,以避免过拟合。 8. 使用预训练模型:利用在大规模数据集上预训练的模型参数作为初始参数,以加速训练过程和提高模型性能。 9. 梯度裁剪:限制梯度的大小,防止梯度爆炸或梯度消失问题,提高模型的稳定性。 10. 提取特征:通过使用预训练的卷积神经网络模型(如VGG、ResNet等),将输入图像转换为特征向量,以便进行后续任务(如分类、检测等)。 这些是一些常见的深度学习shortcut,根据具体任务和数据集的特点,还可以有更多其他的技巧和方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值