您需要让自己成为启动器应用程序。之后,您可以查询包管理器以获取特定包的快捷方式信息:
fun getShortcutFromApp(packageName: String): List<Shortcut> {
val shortcutQuery = LauncherApps.ShortcutQuery()
shortcutQuery.setQueryFlags(FLAG_MATCH_DYNAMIC or FLAG_MATCH_MANIFEST or FLAG_MATCH_PINNED)
shortcutQuery.setPackage(packageName)
return try {
launcherApps.getShortcuts(shortcutQuery, Process.myUserHandle())
.map { Shortcut(it.id, it.`package`, it.shortLabel.toString(), it) }
} catch (e: SecurityException) {
Collections.emptyList()
}
}
可以在这里找到完整的实现:
https://android.jlelse.eu/nhandling-shortcuts-when-building-an-android-launcher-5908d0bb50d2 https://android.jlelse.eu/nhandling-shortcuts-when-building-an-android-launcher-5908d0bb50d2
项目的 Github 链接:
https://github.com/nongdenchet/Shortcuts https://github.com/nongdenchet/Shortcuts
LauncherApps是Android框架提供的一个类:
https://developer.android.com/reference/android/content/pm/LauncherApps.html https://developer.android.com/reference/android/content/pm/LauncherApps.html
“用于检索当前用户的可启动活动列表以及当前用户可见的任何关联的托管配置文件的类,可以使用 getProfiles() 检索这些配置文件。这主要供启动器使用。可以为每个用户查询应用程序由于 PackageManager 不会为其他配置文件提供包广播,因此您可以在此处注册包更改。”