shortcuts实现

1.使用条件
android 7.1以上
2.功能说明
长按桌面app图标出现快捷方式列表,点击可跳转到app的指定界面,用户也可以将其作为一个单独的快捷方式
3.效果截图


3.技术实现
分为两种方式,静态xml配置和动态代码配置,二者各有优劣,xml配置不需要运行app就有了,但是快捷方式不可以取消。动态代码配置更灵活,可以在任意界面新增或者取消快捷方式

静态xml实现方式:新建xml资源文件,将需要内置的快捷方式写入<shortcuts>标签内,其中包括快捷方式的图标、名称、跳转的url等

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
 android:shortcutId="yydb"
 android:enabled="true"
 android:icon="@drawable/fanli"
 android:shortcutShortLabel="@string/yydb"
 android:shortcutLongLabel="@string/yydb"
 android:shortcutDisabledMessage="@string/yydb">
 <intent
    android:action="android.intent.action.VIEW"
    android:data="需要跳转的url"
    android:targetPackage="com.fanli.android.apps"
    android:targetClass="com.fanli.android.basicarc.ui.activity.CustomUrlBridgeActivity" />
    <categories android:name="android.shortcut.conversation"/>
 </shortcut>
 </shortcuts>
          
动态代码实现方式:需要先初始化shortcutinfo,给定快捷方式的属性,然后再调用setDynamicShortcuts告知系统改变快捷方式

ShortcutManager scManager = getSystemService(ShortcutManager.class);
ShortcutInfo scInfoTwo = new ShortcutInfo.Builder(SuperFanActivity.this, "superfan")
        .setLongLabel("超级返")
        .setShortLabel("超级返")
        .setIcon(Icon.createWithResource(SuperFanActivity.this, R.drawable.fanli))
        .setIntents(new Intent[]{new Intent("superFanActivity", Uri.EMPTY, SuperFanActivity.this,SuperFanActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)}).build();
 scManager.setDynamicShortcuts(FanliApplication.shortCutInfos);
</pre>
取消快捷方式:根据快捷方式的id来进行取消,调用disableShortcuts方法实现,只有动态实现的可以取消

scManager.disableShortcuts(Arrays.asList(scInfoTwo.getId(),"该选项已取消"));
scManager.removeDynamicShortcuts(Arrays.asList(scInfoTwo.getId()));


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Flutter 中,可以使用 `Shortcuts`、`ShortcutManager` 和 `RawKeyboardListener` 等组件和类来实现快捷键的使用。 下面是一个简单的示例代码,演示如何在 Flutter 中使用快捷键: ```dart import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); if (kIsWeb || Platform.isLinux || Platform.isMacOS || Platform.isWindows) { SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]); } runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', home: MyHomePage(), ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Flutter Demo'), ), body: Center( child: Shortcuts( shortcuts: { LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyA): const MyShortcut(), LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyB): const MyShortcut(), }, child: RawKeyboardListener( focusNode: FocusNode(), onKey: (RawKeyEvent event) { if (event is RawKeyDownEvent) { if (event.isControlPressed && event.logicalKey == LogicalKeyboardKey.keyA) { // 处理 Ctrl+A 快捷键 MyShortcut.of(context).onPressed(); } else if (event.isControlPressed && event.logicalKey == LogicalKeyboardKey.keyB) { // 处理 Ctrl+B 快捷键 MyShortcut.of(context).onPressed(); } } }, child: Text('Hello, World!'), ), ), ), ); } } class MyShortcut extends StatelessWidget { const MyShortcut({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return SizedBox.shrink(); } void onPressed() { print('MyShortcut onPressed!'); } static MyShortcut of(BuildContext context) { final result = Shortcuts.of(context)?.shortcuts[LogicalKeySet(LogicalKeyboardKey.control, LogicalKeyboardKey.keyA)]; return result is MyShortcut ? result : const MyShortcut(); } } ``` 在上面的代码中,我们使用 `Shortcuts` 组件来定义快捷键,同时使用 `RawKeyboardListener` 组件监听键盘事件。在 `Shortcuts` 中,我们使用一个 `Map` 来定义快捷键的逻辑按键组合和对应的操作,例如 `Ctrl+A` 和 `Ctrl+B` 快捷键。在 `RawKeyboardListener` 中,我们监听所有键盘事件,并根据按下的键盘按键来执行相应的操作。 在上面的代码中,我们还定义了一个 `MyShortcut` 组件,用于处理快捷键事件。在 `MyShortcut` 组件中,我们定义了一个 `onPressed` 方法,用于处理快捷键被按下时的操作。在 `MyShortcut` 中,我们还定义了一个 `of` 方法,用于获取当前上下文中的 `MyShortcut` 实例。 注意:在使用 `Shortcuts` 和 `RawKeyboardListener` 组件时,请务必将其包裹在一个 `FocusNode` 中,以确保它们可以处理键盘事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值