Android 使用 shortcut 创建应用快捷方式

App的快捷方式分为三种:静态快捷方式、动态快捷方式、固定快捷方式

静态快捷方式

静态快捷方式直接在xml文件中定义即可,首先在res\xml文件夹下创建名为shortcut.xml的文件,并填入如下代码

shortcut.xml

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="compose1"
        android:enabled="true"
        android:icon="@drawable/ic_launcher_background"
        android:shortcutShortLabel="@string/compose_shortcut_short_label1"
        android:shortcutLongLabel="@string/compose_shortcut_long_label1"
        android:shortcutDisabledMessage="@string/compose_disabled_message1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.myapplication"
            android:targetClass="com.example.myapplication.ActivityTest" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
<\shortcuts>    

其中

shortcutId	这是必须填写的值,此值必须唯一,否则会将两个相同id的快捷方式识别为同一个
enable		该快捷方式是否可用
icon		该快捷方式显示的小图标
shortcutShortLabel	该值必须为String.xml文件中定义的值,是对快捷方式的简短描述,如果LongLabel的描述过长则会显示ShortLabel
shortcutLongLabel	该值必须为String.xml文件中定义的值,是对快捷方式的描述,当描述过长的时候将会显示ShortLabel中的信息
shortcutDisableMessage	当用户尝试使用被禁用的快捷方式的时候显示的信息,向用户显示为什么被禁用

然后在AndroidManifest文件的activity标签中将shortcut添加进去

    <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>

这样再启动app,长按图标,就会有这样的效果(如果系统支持的话)
一个快捷方式
如果需要多个快捷方式的话,只需要在shortcut.xml文件中再添加一个就好了

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="compose1"
        android:enabled="true"
        android:icon="@drawable/ic_launcher_background"
        android:shortcutShortLabel="@string/compose_shortcut_short_label1"
        android:shortcutLongLabel="@string/compose_shortcut_long_label1"
        android:shortcutDisabledMessage="@string/compose_disabled_message1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.myapplication"
            android:targetClass="com.example.myapplication.ActivityTest" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:shortcutId="compose2"
        android:enabled="true"
        android:icon="@drawable/ic_launcher_background"
        android:shortcutShortLabel="@string/compose_shortcut_short_label1"
        android:shortcutLongLabel="@string/compose_shortcut_long_label1"
        android:shortcutDisabledMessage="@string/compose_disabled_message1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.myapplication"
            android:targetClass="com.example.myapplication.MainActivity" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

动态快捷方式

动态快捷方式是可以在程序运行中进行创建的,创建一个ActivityTest作为第二个页面,在显示页面的时候动态创建快捷方式

ActivityTest.java

public class ActivityTest extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "compose3")
                .setShortLabel("快捷方式二")
                .setLongLabel("这是快捷方式二")
                .setIcon(Icon.createWithResource(this, R.drawable.ic_launcher_foreground))
                .setIntent(new Intent(ActivityTest.this, ActivityTest2.class).setAction(Intent.ACTION_VIEW))
                .build();
        shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
    }
}

在刚安装和打开第一个界面的时候快捷方式是这样的
在这里插入图片描述
在打开第二个界面之后
在这里插入图片描述
快捷方式就会变成这样了,多了一个动态创建的快捷方式。

固定快捷方式

固定快捷方式可以将快捷方式以图标的方式固定在桌面上,创建第三个界面ActivityTest2来创建固定的快捷方式

public class ActivityTest2 extends AppCompatActivity {

    Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShortcutManager shortcutManager =
                        mContext.getSystemService(ShortcutManager.class);

                assert shortcutManager != null;
                if (shortcutManager.isRequestPinShortcutSupported()) {
                    ShortcutInfo pinShortcutInfo =
                            new ShortcutInfo.Builder(mContext, "compose4")
                                    .setShortLabel("这是快捷方式三")
                                    .setLongLabel("这是快捷方式三")
                                    .setDisabledMessage("你真的要禁用吗?")
                                    .setIntent(new Intent(ActivityTest2.this, ActivityTest2.class).setAction(Intent.ACTION_VIEW))
                                    .setIcon(Icon.createWithResource(mContext, R.drawable.ic_launcher_foreground))
                                    .build();

                    // 可以在此创建intent来通知你用户创建了这个固定快捷方式
                    Intent pinnedShortcutCallbackIntent =
                            shortcutManager.createShortcutResultIntent(pinShortcutInfo);

                    //在此完善你接收到广播之后的动作
                    PendingIntent successCallback = PendingIntent.getBroadcast(mContext, /* request code */ 0,
                            pinnedShortcutCallbackIntent, /* flags */ 0);

                    shortcutManager.requestPinShortcut(pinShortcutInfo,
                            successCallback.getIntentSender());
                }
            }
        });

    }
}

然后在打开第三个页面的时候,就可以点击按钮来创建固定快捷方式
在这里插入图片描述
那么快捷方式就会出现在屏幕上了
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值