定位不到目标时如何在Android O上创建快捷方式? [英] How to create shortcuts on Android O, when targetting less than it?

查看:110
本文介绍了定位不到目标时如何在Android O上创建快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android O对快捷方式的工作方式进行了多种更改:

Android O has various changes of how shortcuts work:

https://developer.android.com/preview/behavior-changes .html#as

根据Android O上的最新更改,创建快捷方式的广播意图被完全忽略:

According to recent changes on Android O, the broadcast intent to create shortcuts is completely ignored :

https://developer.android.com/reference/android/content/Intent.html#ACTION_CREATE_SHORTCUT https://developer.android.com/preview/behavior-changes.html#

不再广播com.android.launcher.action.INSTALL_SHORTCUT 对您的应用程序有任何影响,因为它现在是私有的,隐式的 播送.相反,您应该使用 ShortcutManager类中的requestPinShortcut()方法.

The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect on your app, because it is now a private, implicit broadcast. Instead, you should create an app shortcut by using the requestPinShortcut() method from the ShortcutManager class.

例如,这意味着无论您开发了哪个应用程序或用户拥有的启动器,此代码都将不再起作用:

This means this code, for example, won't work anymore, no matter which app you've made or which launcher the user has:

private void addShortcut(@NonNull final Context context) {
    Intent intent = new Intent().putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(context, MainActivity.class).setAction(Intent.ACTION_MAIN))
            .putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut")
            .putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ShortcutIconResource.fromContext(context, R.mipmap.ic_launcher))
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    context.sendBroadcast(intent);
}

当前,即使是Play商店本身也无法创建应用程序的快捷方式(至少在当前版本中为80795200).它甚至什么都没做,甚至对Google的启动器也不起作用.

Currently, even the Play Store itself fails to create shortcuts to apps (at least in the current version: 80795200 ) . It just doesn't do anything, even to Google's launcher.

尽管我非常反对API的这种更改(并在> 此处 此处 )和 此处 ,我想知道仍然需要什么工作.

While I'm very against this change of API (and wrote about it here, here) and here, I'd like to know what would it take to still make it work.

我知道使用

I know there is an API for this, using requestPinShortcut, but this requires the app to target Android O, which means a lot more changes have to be made to make sure the app works there.

我的问题是: 假设您的应用以Android API 25为目标,那么如何在Android O上创建快捷方式?通过使用较新的API的反射是否可能?如果可以,怎么办?

My question is: Suppose your app targets Android API 25, how do you create shortcuts on Android O ? Is it possible by using reflection of the newer API ? If so, how?

推荐答案

正确的方法是调用requestPinShortcut方法.您无需将android O作为目标,但至少需要将SDK编译为26.编译SDK和目标SDK是两个不同的东西.

The right way is to call requestPinShortcut method. You don't need to target android O but you need to have at least compile SDK to 26. Compile SDK and target SDK are two different things.

这篇关于定位不到目标时如何在Android O上创建快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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