如何获取可用的“应用程序快捷方式"特定应用程序? [英] How to get available "app shortcuts" of a specific app?

查看:123
本文介绍了如何获取可用的“应用程序快捷方式"特定应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

从Android的API 25开始,通过长时间点击应用,应用可以在启动器中提供额外的快捷方式:

问题

事实是,我发现的只是您的应用如何向启动器提供这些快捷方式,但我无法确定启动器如何获取它们的列表.

由于它是一个相当新的API,并且大多数用户和开发人员甚至都没有使用它,所以我找不到太多有关它的信息,尤其是因为我想搜索API使用的另一面"./p>

我尝试过的

我尝试阅读文档( 此处 ).我看不到有人提到它.仅提及其他应用程序的一部分,但未提及接收器应用程序(启动器)的部分.

问题

给出应用程序的软件包名称,如何使用新的API获取所有应用程序快捷方式"的列表?

是否可以使用它来请求从其中之一创建固定快捷方式?

解决方案

您需要使自己成为启动器应用程序.之后,您可以查询packagemanager以获得特定软件包的shortcutinfo:

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-何时构建一个Android-launcher-5908d0bb50d2

Github到项目的链接:

https://github.com/nongdenchet/Shortcuts

LauncherApps是Android框架提供的类:

https://developer.android.com/reference/android/content/pm/LauncherApps.html

用于检索当前用户的可启动活动列表以及当前用户可见的任何关联的托管配置文件的类,可以使用getProfiles()进行检索.这主要供启动器使用.可以查询应用程序对于每个用户个人资料.由于PackageManager将不会传送其他个人资料的软件包广播,因此您可以在此处注册软件包更改."

Background

Starting from API 25 of Android, apps can offer extra shortcuts in the launcher, by long clicking on them:

The problem

Thing is, all I've found is how your app can offer those shortcuts to the launcher, but I can't find out how the launcher gets the list of them.

Since it's a rather new API, and most users and developers don't even use it, I can't find much information about it, especially because I want to search of the "other side" of the API usage.

What I've tried

I tried reading the docs (here, for example). I don't see it being mentioned. Only the part of other apps is mentioned, but not of the receiver app (the launcher).

The questions

Given a package name of an app, how can I get a list of all of its "app shortcuts" using the new API?

Is it possible to use it in order to request to create a Pinned Shortcut out of one of them?

解决方案

You need to make yourself the launcher app. After that you can query the packagemanager to get the shortcutinfo for a particular package:

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()
    }

}

A full implementation of this can be found here:

https://android.jlelse.eu/nhandling-shortcuts-when-building-an-android-launcher-5908d0bb50d2

Github link to project:

https://github.com/nongdenchet/Shortcuts

LauncherApps is a class provided by the Android framework:

https://developer.android.com/reference/android/content/pm/LauncherApps.html

"Class for retrieving a list of launchable activities for the current user and any associated managed profiles that are visible to the current user, which can be retrieved with getProfiles(). This is mainly for use by launchers. Apps can be queried for each user profile. Since the PackageManager will not deliver package broadcasts for other profiles, you can register for package changes here."

这篇关于如何获取可用的“应用程序快捷方式"特定应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆