【详细】长按APP图标弹出快捷方式,ShortCut功能实现

29 篇文章 0 订阅
17 篇文章 0 订阅

Shortcuts介绍

Android7.1(API Level 25)及以上系统可以自定义Shortcuts,通过在桌面上长按App Icon弹出Shortcut列表,点击某个shortcut可使用户快捷得打开App里常用的或推荐的任务。国内各个厂商基本上在安卓8.0上集成了该功能。见下图

  

1.1 Shortcuts的简单作用

每个Shortcut可以关联一个或多个intents,每个intent启动一个指定的action; 官方给出了几个可以作为shortcut的例子,比如:

  1. 在地图类app中,指导用户到特定的位置;
  2. 在社交类app中,发送消息给一个朋友;
  3. 在媒体类app中,播放视频的下一片段;
  4. 在游戏类app中,下载最后保存的要点;

在实际开发中,我们具体想让哪些操作作为快捷方式,可自行定义。

1.2 静态实现

1. 在app的AndroidManifest.xml文件中,找到MainActivity,即设置了action为<action android:name=”android.intent.action.MAIN” />,且category设置为 <category android:name=”android.intent.category.LAUNCHER” />的activity;为其添加<meta-data>….</meta-data>指向定义Shortcuts的资源文件。代码如下:

<activity android:name=".MainActivity">
      <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. 创建定义AppShortcuts的资源文件,res/xml/shortcuts.xml文件 

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

    <!-- 第一个静态shortcut -->
    <shortcut
        android:shortcutId="static_one"
        android:enabled="true"
        android:icon="@mipmap/icon_diamond"
        android:shortcutLongLabel="@string/static_one_long_label"
        android:shortcutDisabledMessage="@string/static_disabled_message"
        android:shortcutShortLabel="@string/static_one_short_label">
        <!--
            一个shortcut,当有多个intents与之相关联时,在用户启动该shortcut时,最先呈现给用户的是
            <intent>...</intent>集合中最后一个intent操作事件。
            即这里创建了一个intent的回退栈,最后一个才是被快捷方式打开的那个。
        -->
        <intent
            android:action="android.intent.action.MAIN"
            android:targetPackage="com.example.butterknifetest"
            android:targetClass="com.example.butterknifetest.MainActivity"/>

        <intent
            android:action="android.intent.action.MAIN"
            android:targetPackage="com.example.butterknifetest"
            android:targetClass="com.example.butterknifetest.AndroidTestActivity"/>

        <intent
            android:action="android.intent.action.MAIN"
            android:targetPackage="com.example.butterknifetest"
            android:targetClass="com.example.butterknifetest.TestActivit"/>

    </shortcut>
    <!-- 第二个静态shortcut -->
    <shortcut
        android:shortcutId="static_two"
        android:enabled="true"
        android:icon="@mipmap/icon_star"
        android:shortcutLongLabel="@string/static_two_long_label"
        android:shortcutDisabledMessage="@string/static_disabled_message"
        android:shortcutShortLabel="@string/static_two_short_label">

        <intent
            android:action="android.intent.action.MAIN"
            android:targetPackage="com.example.butterknifetest"
            android:targetClass="com.example.butterknifetest.MainActivity"/>

        <intent
            android:action="android.intent.action.MAIN"
            android:targetPackage="com.example.butterknifetest"
            android:targetClass="com.example.butterknifetest.TestActivity"/>

    </shortcut>


</shortcuts>

3. 属性讲解

以shortcuts元素为根,可以包含多个shortcut元素,每个shortcut元素标示一个shortcut。其中属性分别标示:

  • shortcutId:shortcut唯一标识符,相同的shortcutId会被覆盖。(必设属性)
  • enable:shortcut是否启用,true启用,false是禁用(若设置为false,不如删除掉该快捷方式)(可选属性)
  • icon:显示在快捷方式左边的图标。(可选属性)
  • shortcutLongLabel:当launcher的空间足够时将会显示shortcut的长文本描述,不宜过长,如果过长或未设置时会显示shortcutShortLabel (可选属性)
  • shortcutShortLabel : shortcut的简要说明,这项是必须的。(必设属性)
  • intent : 这里定义快捷方式被点击之后将会打开的intent (必设属性)
  • shortcutDisabledMessage : 当你禁用了shortcut之后,它将不会显示在用户长按应用图标后打开的快捷方式里,但是用户可以把一个快捷方式拖拽到launcher的某个页面成为Pinned Shortcut,被禁用之后这个快捷方式就会显示为灰色,点击这个Pinned Shortcut则会显示一个内容为shortcutDisabledMessage的Toast。(可选属性) 

1.3 动态实现

        代码创建Dynamic shortcut,需要使用API ShortcutManager和ShortcutInfo.Builder;通过ShortcutInfo.Builder新建ShortcutInfo,再通过ShortcutManager添加即可。动态shortcut可以在运行时动态改变内容,无需重写部署App。下面直接看代码,而不在叙述创建的具体步骤:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //要确保API Level 大于等于 25才可以创建动态shortcut,否则会报异常。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            initDynamicShortcuts();
        }
    }

    /**
     * 为App创建动态Shortcuts
     */
    private void initDynamicShortcuts() {
        //①、创建动态快捷方式的第一步,创建ShortcutManager
        ShortcutManager scManager = getSystemService(ShortcutManager.class);
        //②、构建动态快捷方式的详细信息
        ShortcutInfo scInfoOne  = new ShortcutInfo.Builder(this, "dynamic_one")
                .setShortLabel("Dynamic Web site")
                .setLongLabel("to open Dynamic Web Site")
                .setIcon(Icon.createWithResource(this, R.mipmap.tool_music_icon))
                .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com")))
                .build();

       ShortcutInfo scInfoTwo = new ShortcutInfo.Builder(this, "dynamic_two")
                .setShortLabel("Dynamic Activity")
                .setLongLabel("to open dynamic one activity")
                .setIcon(Icon.createWithResource(this, R.mipmap.tool_luck_icon))
                .setIntents(new Intent[]{
                                    new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),//加该FLAG的目的是让MainActivity作为根activity,清空已有的任务
                                    new Intent(DynamicASOneActivity.ACTION)
                            })
                .build();
        //③、为ShortcutManager设置动态快捷方式集合
        scManager.setDynamicShortcuts(Arrays.asList(scInfoOne, scInfoTwo));

        //如果想为两个动态快捷方式进行排序,可执行下面的代码
        ShortcutInfo dynamicWebShortcut = new ShortcutInfo.Builder(this, "dynamic_one")
                .setRank(0)
                .build();
        ShortcutInfo dynamicActivityShortcut = new ShortcutInfo.Builder(this, "dynamic_two")
                .setRank(1)
                .build();

        //④、更新快捷方式集合
        scManager.updateShortcuts(Arrays.asList(dynamicWebShortcut, dynamicActivityShortcut));
    }
}

ShortcutManager API可以帮助我们实现新建、更新、移除快捷方式的操作:

  • 新建:方法setDynamicShortcuts() 可以添加或替换所有的shortcut;方法addDynamicShortcuts() 来添加新的shortcut到列表中,超过最大个数会报异常
  • 更新:方法updateShortcuts(List shortcutInfoList) 更新已有的动态快捷方式;
  • 删除:方法removeDynamicShortcuts(List shortcutIds) 根据动态快捷方式的ID,删除已有的动态快捷方式;方法removeAllDynamicShortcuts() 删除掉app中所有的动态快捷方式;
  • List<ShortcutInfo> getDynamicShortcuts() : 得到所有的动态shortcuts;
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值