Android桌面图标快捷方式

321 阅读3分钟

一、背景

长按桌面图标实现快捷方式最早是iOS提供的功能,而Android最早在Android 7.1版本也提供了对这方面的支持,于是在短时间内,像微信,支付宝,头条等流量级应用都提供了这方面的支持,如下图。
 

现在,长按桌面图标快捷方式已经是很成熟的功能,实现上也比较简单,主要用到的Api就是ShortcutManager。而在具体实现方案上,Android 又给出了两种实现方式,一种是静态,一种是动态。

二、静态方式

静态方式需要使用xml资源,以shortcuts标签的形式引入。对于静态方式,我们首先需要在res目录的xml目录下创建一个xml资源文件,如下所示。

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

&lt;shortcut
    android:enabled="true"
    android:icon="@mipmap/ic_shortcut_scan"
    android:shortcutId="scan"
    android:shortcutShortLabel="扫一扫"&gt;


    &lt;intent
        android:action="android.intent.action.VIEW"
        android:targetClass="com.xzh.app.ScanActivity"
        android:targetPackage="com.xzh.app" /&gt;
    &lt;categories android:name="android.shortcut.conversation" /&gt;
    &lt;capability-binding android:key="actions.intent.SCAN" /&gt;
&lt;/shortcut&gt;


... //省略其他代码




</shortcuts>

</shortcuts>

其中,使用静态方式常见的配置属性如下:

  • enabled:表示这个shortcut是否可用;
  • icon:为快捷图标;
  • shortcutId:快捷方式唯一的id;
  • shortcutShortLabel:短名称;
  • shortcutLongLabel:配置的长名称, launcher会优先选择长名称显示,显示不下会选择短名称;
    • categories:为应用程序的快捷方式执行的操作类型提供分组
  • capability-binding:声明与此快捷方式关联的功能。

 

除此之外,在shortcut标签下也有几个属性需要注意:

  • intent:点击shortcut时的目标页; 
  • targetPackage:指定一个目标应用的包名;
  • targetClass:要跳转的目标类, 需要注意的是,android:action一定要配置,否则可能出现崩溃;
  • categories:目前官方只给提供了android.shortcut.conversation;

 

完成上述操作后,接下来就是在清单文件AndroidManifest.xml里进行配置。具体来说,就是在AndroidManifest.xml的入口Activity中引入shortcuts资源。

<!--引入shortcuts资源-->
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />

完成上述操作后,我们就可以允许工程代码了,效果如下。


 

三、动态方式

静态快捷方式适合毕竟固定的场景,如果快捷方式需要动态配置,那么就需要用到ShortcutManagerCompat或ShortcutManager来实现。

3.1 ShortcutManagerCompat

下面是使用ShortcutManagerCompat实现动态快捷方式的示例代码。

private fun initShortCut() {
//动态方式添加
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
val shortScan = ShortcutInfoCompat.Builder(this, "scan")
.setShortLabel(getString(R.string.short_cut_scan))
.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_shortcut_scan))
.setIntent(Intent(Intent.ACTION_MAIN, null, this, ScanActivity::class.java))
.build()
ShortcutManagerCompat.addDynamicShortcuts(this, mutableListOf(shortScan))
}
}

如果需要更新快捷方式,可以使用updateShortcuts方法,参考代码如下。

private fun updateShortCut() {
val shortScan = ShortcutInfoCompat.Builder(this, "scan")
.setShortLabel(getString(R.string.short_cut_scan))
.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_shortcut_scan))
.setIntent(Intent(Intent.ACTION_MAIN, null, this, MainActivity::class.java))
.build()
ShortcutManagerCompat.updateShortcuts(this, mutableListOf(shortScan))
}

如果要删除快捷方式,需要用到ShortcutManagerCompat的removeDynamicShortcuts删除方法,并且需要使用创建时的唯一id,参考代码如下。

//动态移除
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManagerCompat.removeDynamicShortcuts(
this@MainActivity,
Collections.singletonList("scan")//唯一标识id
)
}

 

3.2 ShortcutManager

除了ShortcutManagerCompat方式外,我们还可以使用ShortcutManager来创建快捷方式,如下所示。

private fun initShortCut() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
val info = ShortcutInfo.Builder(this, "scan")
.setShortLabel(getString(R.string.short_cut_scan))
.setIcon(Icon.createWithResource(this, R.mipmap.ic_shortcut_scan))
.setIntent(intent)
.build()
getSystemService(ShortcutManager::class.java)
.dynamicShortcuts = mutableListOf(info)
}
}

如果需要更新快捷方式,可以使用updateShortcuts方法,如下所示。

private fun updateShortCut() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
val info = ShortcutInfo.Builder(this, "scan")
.setShortLabel(getString(R.string.short_cut_scan))
.setIcon(Icon.createWithResource(this, R.mipmap.ic_shortcut_scan))
.setIntent(intent)
.build()
getSystemService(ShortcutManager::class.java).updateShortcuts(listOf(info))
}
}

如果要删除快捷方式,可以使用removeDynamicShortcuts方法,如下所示。

private fun delShortCut() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
getSystemService(ShortcutManager::class.java)
.removeDynamicShortcuts(listOf("scan"))
}
}

下面是ShortcutManager的常见方法:

  • setDynamicShortcuts():可以添加或替换所有的shortcut;
  • addDynamicShortcuts():来添加新的shortcut到列表中,超过最大个数会报异常
  • updateShortcuts(List shortcutInfoList):更新已有的动态快捷方式;
  • removeDynamicShortcuts(List shortcutIds):根据动态快捷方式的ID,删除已有的动态快捷方式;
  • removeAllDynamicShortcuts():删除掉app中所有的动态快捷方式;
  • getDynamicShortcuts():得到所有的动态shortcuts;

到此,快捷方式的静态和动态添加方式就介绍完成了。

本文来源:Android桌面图标快捷方式