Android 13 为应用创建快捷方式

发布于:2024-06-29 ⋅ 阅读:(43) ⋅ 点赞:(0)

参考 developer.android.google.cn 创建快捷方式

来自官网的说明:

  • 静态快捷方式 :最适合在用户与应用互动的整个生命周期内使用一致结构链接到内容的应用。由于大多数启动器一次仅显示四个快捷方式,因此静态快捷方式有助于以一致的方式执行日常任务,例如,如果用户希望以特定方式查看日历或电子邮件。
  • 动态快捷方式 :用于应用中与上下文相关的操作。上下文相关快捷方式针对用户在应用中执行的操作量身打造。例如,如果您构建的游戏允许用户在启动时从当前关卡开始,您需要经常更新该快捷方式。借助动态快捷方式,您可以在每次用户通关时更新快捷方式。
  • 固定快捷方式 :用于用户驱动的特定操作。例如,用户可能需要将特定网站固定到启动器。这种方式是有益的,因为它可让用户执行自定义操作,例如一步导航到网站,这比使用浏览器的默认实例速度更快。

原生系统上,长按应用图标显示快捷方式,点击快捷方式就打开应用的某个页面。
在这里插入图片描述

创建静态快捷方式

1.清单文件添加

在应用的主页面添加如下,shortcuts 就是要配置的文件。

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

主页面就是配置了 android.intent.action.MAINandroid.intent.category.LAUNCHER 的 Activity 。
示例:

        <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/shortcuts" />
        </activity>

2.创建shortcuts.xml

创建 res/xml/shortcuts.xml 文件,配置如下,

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

    <shortcut
        android:enabled="true"
        android:icon="@drawable/shape_oval_sweep"
        android:shortcutDisabledMessage="@string/shortcut_disabled_message1"
        android:shortcutId="id11"
        android:shortcutLongLabel="@string/shortcut_long_label1"
        android:shortcutShortLabel="@string/shortcut_short_label1">
        <intent
            android:action="android.intent.action.VIEW"
            android:data="shortcut1"
            android:targetClass="com.test.luodemo.appwidget.ShortcutActivity"
            android:targetPackage="com.test.luodemo" />
    </shortcut>

    <shortcut
        android:enabled="true"
        android:shortcutDisabledMessage="@string/shortcut_disabled_message2"
        android:shortcutId="id22"
        android:icon="@drawable/shape_oval_liner"
        android:shortcutLongLabel="@string/shortcut_long_label2"
        android:shortcutShortLabel="@string/shortcut_short_label2">
        <intent
            android:action="android.intent.action.VIEW"
            android:data="shortcut2"
            android:targetClass="com.test.luodemo.appwidget.ShortcutActivity"
            android:targetPackage="com.test.luodemo" />
    </shortcut>

</shortcuts>

创建了两个快捷方式。

  • android:enabled :是否可用,默认值为 true。如果将其设置为 false,请设置 android:shortcutDisabledMessage,说明停用该快捷方式的原因。如果您认为自己不需要提供此类消息,请从 XML 文件中完全移除该快捷方式。
  • android:shortcutDisabledMessage :用户尝试启动已停用的快捷方式时显示在支持的启动器中的消息。如果 android:enabled 为 true,则此属性的值无效。
  • android:shortcutId :字符串。
  • android:icon :快捷方式的图标。
  • android:shortcutShortLabel :快捷方式的简短说明,长度限制为 10 个字符。
  • android:shortcutLongLabel :快捷方式的详细说明。如果空间足够,会显示此值,而不是 android:shortcutShortLabel ,长度限制为 25 个字符。

intent 内部元素

  • android:targetClass :跳转的页面;
  • android:targetPackage :跳转的应用包名。
  • android:data :携带参数,方便区分是哪个快捷方式,跳转的页面可以通过 getIntent().intent.getDataString() 得到数据。

搞定,运行效果
在这里插入图片描述

创建动态快捷方式

动态的意思就是,需要的时候添加,不需要时删除。

写两个 Button ,一个创建,一个删除。

都是用 androidx.core.content.pm.ShortcutManagerCompat

创建

很简单,一目了然。功能是 跳转到设置查看本应用的通知。

import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;

        ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(mContext, "iddynamic")
                .setShortLabel("此应用的通知")
                .setLongLabel("动态快捷方式长描述")
                .setIcon(IconCompat.createWithResource(mContext, R.drawable.shape_ring))
                .setIntent(new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
                        .putExtra(Settings.EXTRA_APP_PACKAGE,mContext.getPackageName()))
                .build();

        ShortcutManagerCompat.pushDynamicShortcut(mContext, shortcut);

删除

根据 id 删除单个,

List<String> mList = new ArrayList<>();
mList.add(shortcutId);
ShortcutManagerCompat.removeDynamicShortcuts(mContext, mList);

删除所有,

ShortcutManagerCompat.removeAllDynamicShortcuts(mContext);

效果
在这里插入图片描述

创建桌面快捷方式

使用 ShortcutManager 实现,

    private void addPinShortcut(){
        ShortcutManager shortcutManager = mContext.getSystemService(ShortcutManager.class);

        if (shortcutManager.isRequestPinShortcutSupported()) {
            //跳转应用消息
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", mContext.getPackageName(), null);
            intent.setData(uri);

            // Enable the existing shortcut with the ID "my-shortcut".
            ShortcutInfo pinShortcutInfo =
                    new ShortcutInfo.Builder(mContext, "my-shortcut")
                            .setIcon(Icon.createWithResource(mContext, R.drawable.shape_rectangle_corners))
                            .setShortLabel("固定快捷方式")
                            .setLongLabel("固定快捷方式长描述")
                            .setIntent(intent)
                            .build();

            Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo);
            PendingIntent successCallback;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
                successCallback = PendingIntent.getActivity(mContext, 101, pinnedShortcutCallbackIntent, PendingIntent.FLAG_IMMUTABLE);
            } else {
                successCallback = PendingIntent.getActivity(mContext, 101, pinnedShortcutCallbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            }

            boolean ret = shortcutManager.requestPinShortcut(pinShortcutInfo,successCallback.getIntentSender());
            Log.d(TAG , "addPinShortcut -- ret : " + ret);
        }
    }

本例功能是跳转掉设置,打开此应用的应用信息页面。

运行效果
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到