安卓添加快捷方式

长按APP图标时,出现一些选项,比如长按微信就有的扫一扫

本博客编写与2022年5月6日,安卓版本为安卓12。效果类似于相机这样的,具体我的忘记截图了。
在这里插入图片描述

首先,在res/values/strings.xml中写:(具体写什么看你自己,核心是:最多写4组名字,每组一个长标题,一个短标题,最多共计8个,当然仅写一组也可以。如果长标题过长,手机就会显示短标题,因此往往短标题要小于长标题,但我的例子中,标题都很短,因此名字都一样,具体命名非常随意)。

    <string name="one">专业</string>
    <string name="two">专业</string>
    <string name="first_short">扫一扫</string>
    <string name="first_long">扫一扫</string>
    <string name="second_short">视频</string>
    <string name="second_long">视频</string>
    <string name="third_short">人像</string>
    <string name="third_long">人像</string>

然后新建xml文件夹在res文件中,在xml文件夹里新建一个xml文件,命名随意,我这里命名了f.xml。
在这里插入图片描述
内容如下:
注意:shortcuts这个名字还不清楚是否可以换成其他名,但是小标签shortcut必须这么写,否则不显示快捷方式,intent里的targetPackage是包名,targetClass是想要挑战到的界面的类名,需要自己修改,shortcut标签中的东西要根据自己的需求改,categories标签必须这么写,好像还没有其他写法,这么写即可,不用管,另外标签的内容缺一不可,决不能把shortcutId之类的删除,否则不显示。我这里写了6个shortcut,但是实际只会显示前4个标签。

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutId="second"
        android:shortcutLongLabel="@string/first_long"
        android:shortcutShortLabel="@string/first_short">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.app4.MainActivity"
            android:targetPackage="com.example.app4" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutId="third"
        android:shortcutLongLabel="@string/second_long"
        android:shortcutShortLabel="@string/second_short">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.app4.MainActivity"
            android:targetPackage="com.example.app4" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutId="four"
        android:shortcutLongLabel="@string/third_long"
        android:shortcutShortLabel="@string/third_short">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.app4.MainActivity"
            android:targetPackage="com.example.app4" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutId="five"
        android:shortcutLongLabel="@string/one"
        android:shortcutShortLabel="@string/two">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.app4.MainActivity"
            android:targetPackage="com.example.app4" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutId="six"
        android:shortcutLongLabel="@string/third_long"
        android:shortcutShortLabel="@string/third_short">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.app4.MainActivity"
            android:targetPackage="com.example.app4" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutId="seven"
        android:shortcutLongLabel="@string/third_long"
        android:shortcutShortLabel="@string/third_short">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.app4.MainActivity"
            android:targetPackage="com.example.app4" />
        <categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>

在AndroidMenifest.xml文件中,
注意:必须在有android.intent.action.MAIN和android.intent.category.LAUNCHER这个里写,写其他地方无用。这里的resource里的是路径名,根据自己的改;name属性就这么写的,不用动。
写入:

            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/f" />

这块完整代码:

        <activity
            android:name=".MainActivity"
            android:exported="true" >
            <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/f" />
        </activity>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值