Android 7.1 允许 App 自定义 Shortcuts,类似 iOS 的 3D touch。通过在桌面长按 App 弹出 Shortcut 列表,点击某个 Shortcut 快速进入某项操作,同时 Shortcut 可以拖动到桌面进行固定。
目前仅 7.1 系统桌面支持该特性,三方桌面需要通过 LauncherApps 这个 API 支持此功能,本文主要介绍三方桌面如何接入此特性。
可以下载 ShortcutViewer 查看效果: Google Play , 应用宝 。截图如下:
关于 Shortcuts 的全面介绍可见:
第一篇: Android 7.1 新特性 Shortcuts 介绍
第二篇: Android 7.1 新特性 Shortcuts 一些实践和目前的问题
如果不了解 Shortcuts 基本使用建议先看上面第一篇。
在 AndroidManifest.xml 的 Main Launcher 对应的 Activity 内添加 android.intent.category.HOME 这个 category,表示此应用为桌面,如下:
<application ……> <activityandroid:name=".MainActivity"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> <categoryandroid:name="android.intent.category.HOME" /> </intent-filter> </activity> </application>
必须在 Main Launcher 对应的 Activity 内设置,其中 android.intent.category.HOME 表示这是个桌面程序。
Android SDK LauncherApps API 要求必须是默认桌面才有权限获取到所有应用 Shortcuts 信息。
按 Home 键退到后台会提示是否将此应用设置为桌面,选择”始终”(某些手机可能是默认)。
或者通过 设置-应用-配置应用-主屏幕应用 ,选择自己的应用作为默认桌面。
通过 LauncherApps.getShortcuts 获取各 App Shortcuts 信息:
LauncherAppslauncherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
if (!launcherApps.hasShortcutHostPermission()) {
// Don't have permission, you may need set this app as default desktop.
return;
}
PackageManagerpackageManager = context.getPackageManager();
IntentmainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfoList;
if (packageManager == null || CollectionUtils
.isEmpty(resolveInfoList = packageManager.queryIntentActivities(mainIntent, 0))) {
// No Main&Launcher Activity
return;
}
// Get ShortcutInfo for every app
Set<String> packageNameSet = new HashSet<>();
for (ResolveInfoinfo : resolveInfoList) {
ApplicationInfoapplicationInfo;
if (info == null || info.activityInfo == null
|| (applicationInfo = info.activityInfo.applicationInfo) == null || !applicationInfo.enabled
|| packageNameSet.contains(applicationInfo.packageName)) {
continue;
}
packageNameSet.add(applicationInfo.packageName);
int queryFlags = ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_MANIFEST
| ShortcutQuery.FLAG_MATCH_PINNED;
List<ShortcutInfo> shortcutInfoList = launcherApps.getShortcuts(
new ShortcutQuery().setPackage(applicationInfo.packageName).setQueryFlags(queryFlags),
UserHandle.getUserHandleForUid(applicationInfo.uid));
……
}
上面主要步骤包括:
(1) 通过 LauncherApps.hasShortcutHostPermission() 判断是否拥有获取 shortcuts 信息的权限;
(2) 通过 PackageManager.queryIntentActivities(…) 得到所有已安装应用,并且含有 ACTION_MAIN&CATEGORY_LAUNCHER Intent 的 ResolveInfo ;
(3) 遍历每个符合条件的 ResolveInfo ,通过 LauncherApps.getShortcuts(…) 得到其 shortcuts 信息。
注意:这里也可以通过其他方式得到所有适合在桌面显示的 ApplicationInfo ,而通过 PackageManager.queryIntentActivities(…) 是性能最优的方式。
int queryFlags = ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_MANIFEST | ShortcutQuery.FLAG_MATCH_PINNED; List<ShortcutInfo> shortcutInfoList = launcherApps.getShortcuts( new ShortcutQuery().setPackage(applicationInfo.packageName).setQueryFlags(queryFlags), UserHandle.getUserHandleForUid(applicationInfo.uid));
LauncherApps.getShortcuts(LauncherApps.ShortcutQuery query, UserHandle user) 的两个参数分别表示查询条件和查询的 App 对应的 UserHandle。
当然这个特性仅对 Android SDK 7.1 及以上才有效,所以最好先判断下系统 API 版本才开始调用 LauncherApps 相关 API。
100offer 寒冬里帮你找到更好的工作!
互联网行业的年轻人,他们面对着怎样的职业瓶颈、困惑与未来选择?过去,这鲜有人关心。资深的职场人,也多半优先选择熟人去推荐机会。
100offer 致力于改变现状,帮互联网行业最好的人才发现更好的机会。使用 100offer.com或 100offer App,可以一周内获得中国、美国等数千家优质企业的工作机会。
平安陆金所 (已经用了三年的理财产品)
Android 高薪内推 (均年薪 50w+,另加股票期权)