​参考文章​

什么是ShortCuts

  • ​App ShortCuts​​是在图标位置长按app图标出现的快捷方式,通过这一功能可以将对某些应用中的功能进行快速使用,同时也可以拖拽到桌面成为桌面的快捷方式。每个快捷方式都可以携带一个或者多个intent,当用户点击快捷方式时,每个intent都会触发对应的操作

ShortCuts的具体使用

  • 静态的快捷方式: 其直接会打包到 apk 或 apk bundle 中,安装完应用便存在快捷方式入口
  • 动态的快捷方式: 只有在应用运行时才会创建,可以随时的更新、添加和删除对应的快捷方式
  • 桌面快捷方式: 在用户授权的情况下,可以主动的添加快捷方式到桌面,同样可以拷贝动态和静态的快捷方式到桌面
  • 注意:
  • shortcuts对于一个应用程序一般最多可以创建五个快捷方式(包括静态和动态),但是大多数设备上只能展示出四个
  • 桌面的快捷方式的数目不做出任何限制,但是需要用户主动删除
静态注册
  • 最适合那种在整个生命周期中,intent不会进行改变 ,始终完成同一种行为,例如联系人
  • 创建资源文件res/xml/shortcuts.xml
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="static shortcut"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:shortcutShortLabel="@string/shortcut_short_name"
android:shortcutLongLabel="@string/shortcut_long_name"
android:shortcutDisabledMessage="@string/shortcut_disable_msg">
<intent
android:action="android.media.action.IMAGE_CAPTURE">
<extra android:name="android.intent.extras.CAMERA_FACING" android:value="0" />
</intent>
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
  • 然后在AndroidManifest.xml文件中的主活动中添加如下代码块即可
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts"/>

标签

作用

android:shortcutId

shortcut的唯一标识,更新删除都需要这个唯一的关键字

android:shortcutShortLabel

控制在10个字符内的简明短语

android:shortcutLonngLabel

控制在25个字符内的扩展短语

android:enabled

控制桌面快捷方式是否被被禁用

android:icon

左侧显示的图标

android:action

intent跳转的action

android:targetPackage

跳转页面的包名

android:targetClass

跳转页面的完整路径

  • 注意:
  • intent携带信息需要进行解析

动态注册

  • 一般对intent较为敏感,intent可能在应用运行中发生改变,需要更新快捷方式
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_short_cuts_test);
setDynamicShortCuts();
}
@TargetApi(Build.VERSION_CODES.N_MR1)
private ShortcutInfo createShortCutInfo(){
return new ShortcutInfo.Builder(this, "one")
.setShortLabel(getString(R.string.shortcut_short_name))
.setLongLabel(getString(R.string.shortcut_long_name))
.setIcon(Icon.createWithResource(this,R.mipmap.ic_launcher))
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("www.baidu.com")))
.build();
}
private void setDynamicShortCuts(){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
List<ShortcutInfo> shortcutInfo = new ArrayList<>();
shortcutInfo.add(createShortCutInfo());
if (shortcutManager != null) {
shortcutManager.setDynamicShortcuts(shortcutInfo);
}
}
}

桌面快捷方式

  • 允许用户自定义跳转intent,桌面快捷方式支持在设备上单独的icon展示
  • 使用步骤:
  • 验证设备是否支持快捷方式
  • 根据快捷方式是否存在使用下面的方式之一来创建ShortcutInfo对象
  • 如果存在,就使用现有的快捷方式的id创建shortcutInfo对象(系统会自动进行查找)
  • 如果要固定新的快捷方式,创建具有新的id的shortcutInfo对象,
  • 通过requestPinShortcut()将快捷方式固定到设备桌面上,在此过程中可以传入 pendingIntent对象(快捷方式固定式该对象告知应用)
private void createPinnedShortCuts(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if ((shortcutManager != null) && (shortcutManager.isRequestPinShortcutSupported())) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "two")
.setShortLabel(getString(R.string.shortcut_short_name))
.setLongLabel(getString(R.string.shortcut_long_name))
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setIntent(intent)
.build();
Intent pinnedShortCutCallback = shortcutManager.createShortcutResultIntent(shortcutInfo);
PendingIntent successCallback = PendingIntent.getBroadcast(this, 0,
pinnedShortCutCallback, 0);
boolean b = shortcutManager.requestPinShortcut(shortcutInfo, successCallback.getIntentSender());
}
}
}

系统设置的更改

  • 在项目中需要针对夜间主题来更新相应的shortcuts的图标,需要创建广播监听Intent.ACTION_LOCAL_CHANGED