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
    评论
### 回答1: 在 Node.js 中创建桌面快捷方式的方法有以下几种: 1. 使用 `node-shortcut` 库: 可以使用 `node-shortcut` 库来创建桌面快捷方式。安装方法: ``` npm install node-shortcut ``` 然后,可以使用以下代码来创建快捷方式: ``` const shortcut = require('node-shortcut'); shortcut.create('C:\\path\\to\\your\\application.exe', 'My Shortcut', { target: 'C:\\path\\to\\your\\application.exe', cwd: 'C:\\path\\to\\your', icon: 'C:\\path\\to\\your\\icon.ico', description: 'My application shortcut', workingDir: 'C:\\path\\to\\your' }, function(err) { if (err) { console.error(err); } else { console.log('Shortcut created successfully'); } }); ``` 2. 使用 `child_process` 模块的 `exec` 方法: 可以使用 Node.js 的 `child_process` 模块的 `exec` 方法来执行系统命令,从而创建快捷方式。安装方法: ``` npm install child_process ``` 然后,可以使用以下代码来创建快捷方式: ``` const childProcess = require('child_process'); const targetPath = 'C:\\path\\to\\your\\application.exe'; const shortcutPath = 'C:\\path\\to\\your\\shortcut.lnk'; const iconPath = 'C:\\path\\to\\your\\icon.ico'; const description = 'My application shortcut'; const command = `powershell -Command "$WshShell = New-Object -ComObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut('${shortcutPath}'); $Shortcut.TargetPath = '${targetPath}'; $Shortcut.IconLocation = '${iconPath}'; $Shortcut.Description = '${description}'; $Shortcut.Save();"`; childProcess.exec(command, function(err, stdout, stderr) { if (err) { console.error(err); } else { console.log('Shortcut created successfully'); } }); ``` 3. 使用 `node-windows` 库: 可以使用 `node-windows ### 回答2: 在Node.js中创建桌面快捷方式可以通过使用`windows-shortcuts`模块实现。以下是使用该模块在Windows操作系统中创建桌面快捷方式的步骤: 首先,通过以下命令安装`windows-shortcuts`模块: ```bash npm install windows-shortcuts ``` 接下来,在Node.js文件中导入`windows-shortcuts`模块: ```javascript const createShortcut = require('windows-shortcuts').create; ``` 然后,创建一个新的快捷方式对象,指定目标文件路径、快捷方式名称和输出路径: ```javascript const shortcut = createShortcut({ target: 'C:\\path\\to\\your\\file.exe', name: 'My Shortcut', output: 'C:\\Users\\Username\\Desktop' // 保存到桌面 }); ``` 在此示例中,`target`属性指定了所需创建快捷方式的文件路径,`name`属性指定了快捷方式的名称(你可以根据需要自定义),`output`属性指定了快捷方式的输出路径。 最后,使用`shortcut.save()`方法保存快捷方式文件: ```javascript shortcut.save(); ``` 此方法将在指定的输出路径中创建一个快捷方式文件。 完整的创建桌面快捷方式的示例代码如下: ```javascript const createShortcut = require('windows-shortcuts').create; const shortcut = createShortcut({ target: 'C:\\path\\to\\your\\file.exe', name: 'My Shortcut', output: 'C:\\Users\\Username\\Desktop' // 保存到桌面 }); shortcut.save(); ``` 请注意,此方法仅适用于Windows操作系统。如果你的应用程序需要在其他操作系统上创建桌面快捷方式,你需要使用相应的模块或方法来实现。 ### 回答3: 在Node.js中,可以使用第三方模块`node-os-utils`来创建桌面快捷方式。以下是基本的步骤: 1. 首先,确保已在您的项目中安装了`node-os-utils`模块。您可以使用以下命令来安装该模块: ``` npm install node-os-utils ``` 2. 在您的Node.js文件中,引入所需的模块: ```javascript const { Shortcut } = require('node-os-utils'); ``` 3. 使用`Shortcut`类创建一个`shortcut`对象: ```javascript const shortcut = new Shortcut(); ``` 4. 使用`create`方法创建一个桌面快捷方式。您需要提供快捷方式的目标路径、名称和图标路径(可选): ```javascript const targetPath = '/path/to/your/application.exe'; const shortcutName = 'My Application'; const iconPath = '/path/to/your/application.ico'; shortcut.create(targetPath, shortcutName, iconPath) .then(() => { console.log('桌面快捷方式创建'); }) .catch((error) => { console.error('创建桌面快捷方式时出错:', error); }); ``` 5. 运行上述Node.js文件,如果一切顺利,您将在桌面上看到一个名为`My Application`的快捷方式。 请注意,以上步骤假设您的操作系统是Windows。如果您使用的是其他操作系统,可能需要使用其他模块或方法来创建桌面快捷方式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值