Android 13 为应用创建快捷方式

参考 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);
        }
    }

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

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

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Android 动态创建快捷方式的步骤: 1. 首先,您需要在 AndroidManifest.xml 文件中声明您的快捷方式。在应用程序的 <application> 标记内部,添加以下内容: ```xml <activity android:name=".MyShortcutActivity" android:label="@string/shortcut_label"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> ``` 2. 创建一个新的 Activity 类 MyShortcutActivity,该类将处理创建快捷方式的请求。在 onCreate() 方法中,您可以设置快捷方式的属性,例如快捷方式 ID、快捷方式标签和快捷方式图标。 ```java public class MyShortcutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置快捷方式 ID 和标签 String shortcutId = "my_shortcut"; String shortcutLabel = "My Shortcut"; // 创建快捷方式意图 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW); shortcutIntent.setClassName(this, MainActivity.class.getName()); // 创建快捷方式 ShortcutInfo shortcut = new ShortcutInfo.Builder(this, shortcutId) .setShortLabel(shortcutLabel) .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon)) .setIntent(shortcutIntent) .build(); // 添加快捷方式 ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut)); // 结束 Activity finish(); } } ``` 3. 在您的应用程序中,您可以通过调用 ShortcutManager 的 setDynamicShortcuts() 方法来添加动态快捷方式。在这个例子中,我们只添加了一个快捷方式,但您可以添加多个快捷方式。 ```java ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut)); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值