Android创建应用快捷方式( 实现 3D Touch)

快捷方式(ShortCut)可帮助用户快速访问应用程序的各个部分,从而为用户提供特定类型的内容。Android7.1(Android的API版本号大于等于25)开始支持创建应用快捷方式(ShortCut)。
根据官方文档描述,创建快捷方式有三种:

  • 静态快捷方式: 最适合在用户与应用程序互动的整个生命周期中使用一致结构链接到内容的应用程序。由于大多数启动器一次 只能显示四个快捷方式,因此静态快捷方式对于常见活动很有用。例如,如果用户希望以特定方式查看其日历或电子邮件,则使用静态快捷方式可确保他们执行例行任务的经验是一致的。
  • 动态快捷键方式: 用于上下文相关的应用程序中的操作。例如,如果您构建的游戏允许用户在启动时从其当前级别开始,则快捷方式将需要经常更新。使用动态快捷方式允许每次用户清除级别时更新该快捷方式。
  • 固定的快捷方式: 用于特定的,用户驱动的操作。例如,用户可能想要将特定网站固定到启动器。这是有好处的,因为与使用浏览器的默认实例相比,它使用户可以更快地执行自定义操作,例如一步导航到网站。
注意:静态和动态创建快捷方式是通过长按应用启动图片弹出快捷列表;固定的快捷方式,是在桌面上创建一个固定的图标

静态快捷方式

效果图

长按DemoApp图标显示快捷名称
在这里插入图片描述

实现步骤:

  1. 在xml文件夹里创建shortcuts.xml
    在这里插入图片描述

shortcuts.xml代码

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 静态方式创建快捷键 -->
    
    <!--  
         <string name="static_disabled_message">快捷方式不可使用时显示的名字</string>
         <string name="static_shortcut_long_label">打开主界面</string>
         <string name="static_shortcut_short_label">测试短标题</string>
    -->
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutDisabledMessage="@string/static_disabled_message"
        android:shortcutId="shortcutId_1"
        android:shortcutLongLabel="@string/static_shortcut_long_label"
        android:shortcutShortLabel="@string/static_shortcut_short_label"
        >

        <categories android:name="android.shortcut.conversation" />

        <!-- 返回目标 activity,注意要放目标 activity在上面-->
        <intent
            android:action="example.demo.app.activity.ViewToBitmapActivity"
            android:targetClass="example.demo.app.activity.ViewToBitmapActivity"
            android:targetPackage="example.demo.app" />

        <!--目标 activity-->
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="example.demo.app.activity.MainActivity"
            android:targetPackage="example.demo.app" />


    </shortcut>

</shortcuts>
  1. 应用的清单文件(AndroidManifest.xml)找到应用程序默认启动Activity,向此Activity添加元素,该元素引用定义了应用程序快捷方式的资源文件:
    代码如下:
 <application
        android:name="example.demo.app.application.App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="example.demo.app.activity.ShortCutsActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:hardwareAccelerated="true"
            android:theme="@style/SplashAppTheme">
            <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>
        <!-- 主界面 -->
        <activity android:name="example.demo.app.activity.MainActivity" />

        <!-- 注意,此activity作为静态快捷键方式返回目标页,需要设置 android:permission -->
        <activity
            android:name="example.demo.app.activity.ViewToBitmapActivity"
            android:permission="example.demo.demoapplication.activity.ViewToBitmapActivity" />
    </application>

静态创建快捷方式属性说明:

  • android:shortcutId 和 android:shortcutShortLabel是必须填值,其他属性值是可选的;
  • android:shortcutId:字符串文字,表示对象对其执行操作时的快捷方式。 ShortcutManager,不能将此属性的值设置为资源字符串,例如@string/shortcut_Id。
  • android:shortcutShortLabel:简短的短语,描述了快捷方式的用途。尽可能将快捷方式的“简短描述”的长度限制为10个字符。此属性的值必须是资源字符串,例如 @string/shortcut_short_label。
  • android:shortcutLongLabel:描述该快捷方式用途的扩展短语。如果有足够的空间,启动器将显示此值而不是android:shortcutShortLabel。如果可能,将快捷方式的“详细描述”的长度限制为25个字符。此属性的值必须是资源字符串,例如 @string/shortcut_long_label。
  • android:shortcutDisabledMessage:当用户尝试启动禁用的快捷方式时,在受支持的启动器中显示的消息。该消息应向用户说明为什么现在禁用了快捷方式。如果android:enabled为,则此属性的值无效true。此属性的值必须是资源字符串,例如 @string/shortcut_disabled_message。

动态快捷方式:

动态快捷键提供了指向应用程序中特定于上下文的特定操作的链接。这些操作可能会在您的应用使用之间发生变化,甚至在您的应用运行时也会发生变化。动态快捷方式的不错选择包括呼叫特定人员,导航至特定位置以及从用户的最后保存点加载游戏。

ShortcutManager API可让您对动态快捷方式完成以下操作:

  • 发布:使用 setDynamicShortcuts()重新定义动态快捷键的完整列表,或者使用 addDynamicShortcuts() 以增加动态快捷键的现有列表。
  • 更新:使用 updateShortcuts()方法。
  • *删除:使用删除一组动态快捷方式 removeDynamicShortcuts(),或使用 删除所有动态快捷方式 removeAllDynamicShortcuts()。
    有关对快捷方式执行操作的更多信息,请阅读 管理快捷方式ShortcutManager参考。

效果图

动态创建快捷方式

java代码

@RequiresApi(api = Build.VERSION_CODES.N_MR1) // 版本注解
public class ShortCutsActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createDynamicShortcuts();

    }

    /**
     * 动态创建快捷方式
     */
    private void createDynamicShortcuts() {
        ShortcutManager systemService = getSystemService(ShortcutManager.class);
        ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "shortcut_id_1")
                .setShortLabel("website")
                .setLongLabel("Open the website")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/")))
                .build();

        systemService.addDynamicShortcuts(Arrays.asList(shortcutInfo));
    }

}

固定的快捷方式

在Android 8.0(API级别26)及更高版本上,您可以创建固定的快捷方式。与静态和动态快捷方式不同,固定的快捷方式在支持的启动器中显示为单独的图标。图1显示了这两种快捷方式之间的区别。
在这里插入图片描述
图1.应用程序快捷方式和固定快捷方式的外观

要使用您的应用将快捷方式固定到受支持的启动器,请完成以下步骤序列:

  1. 使用 isRequestPinShortcutSupported()验证应用程序内设备的默认启动支持钉扎快捷键。
  2. ShortcutInfo根据快捷方式是否已存在,以两种方式之一创建对象:
  • 如果快捷方式已经存在,请创建一个 ShortcutInfo仅包含现有快捷方式ID的对象。系统会自动查找并固定与快捷方式相关的所有其他信息。
  • 如果要固定新的快捷方式,请创建一个ShortcutInfo对象,其中包含新快捷方式 的ID,意图和简短标签。

注意:因为系统会自动对固定的快捷方式执行 备份和还原,所以这些快捷方式的ID应该包含稳定,恒定的字符串或服务器端标识符,而不是本地生成的标识符,这些标识符在其他设备上可能没有意义。

  1. 尝试通过调用将快捷方式固定到设备的启动器 requestPinShortcut()。在此过程中,您可以传入一个 PendingIntent对象,该对象仅在成功固定快捷方式后才通知您的应用。

注意:如果用户不允许将快捷方式固定在启动器上,则您的应用不会收到回调。
固定快捷方式后,您的应用可以使用 updateShortcuts()方法更新其内容 。有关更多信息,请阅读 更新快捷方式。

  1. 以下代码段演示了如何创建固定的快捷方式:

注意:ShortcutManager该类的实例必须 Context.getSystemService(Class)与ShortcutManager.class或 Context.getSystemService(String)一起使用 Context.SHORTCUT_SERVICE。

效果图

固定的快捷方式

Java 代码

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
public class ShortCutsActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPinShortcut();

    }

    /**
     * 固定快捷方式
     */
    private void requestPinShortcut() {

        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (shortcutManager != null && shortcutManager.isRequestPinShortcutSupported()) {
                ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "shortcut_id_pin")
                        .setShortLabel("website")
                        .setLongLabel("Open the website")
                        .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                        .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/")))
                        .build();

                Intent shortcutResultIntent = shortcutManager.createShortcutResultIntent(shortcutInfo);
                PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, shortcutResultIntent, 0);
                shortcutManager.requestPinShortcut(shortcutInfo, broadcast.getIntentSender());
            }
        }

    }

}

参考官网Shortcut相关文档链接如下:

  1. Android 7.1 for Developers
  2. Create shortcuts
  3. ShortcutManager
  4. Manage shortcuts

欢迎关注我的公众号,不定期推送优质的文章,
微信扫一扫下方二维码即可关注。
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值