and*_*per 5 android shortcut android-intent android-8.0-oreo
Android O对快捷方式的工作方式进行了各种更改:
https://developer.android.com/preview/behavior-changes.html#as
根据Android O的最新变化,完全忽略了创建快捷方式的广播意图:
https://developer.android.com/reference/android/content/Intent.html#ACTION_CREATE_SHORTCUT https://developer.android.com/preview/behavior-changes.html#as
com.android.launcher.action.INSTALL_SHORTCUT广播对您的应用不再有任何影响,因为它现在是一个私有的隐式广播.相反,您应该使用ShortcutManager类中的requestPinShortcut()方法创建应用程序快捷方式.
这意味着,例如,无论您使用哪个应用或用户具有哪个启动器,此代码都将无法使用:
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);
}
Run Code Online (Sandbox Code Playgroud)
目前,即使Play商店本身也无法创建应用程序的快捷方式(至少在当前版本中:80795200).即使对谷歌的发射器,它也没有做任何事情.
虽然我很反对API的这种变化(并且写了关于它在这里,在这里),并在这里,我想知道那会是什么采取仍然使它工作.
我知道有一个API,使用requestPinShortcut,但这需要应用程序以Android O为目标,这意味着必须进行更多更改以确保应用程序在那里工作.
我的问题是:假设您的应用针对Android API 25,如何在Android O上创建快捷方式?是否可以通过使用较新API的反映?如果是这样,怎么样?
似乎我永远打败了这个,但是......
if(Build.VERSION.SDK_INT < 26) {
...
}
else {
ShortcutManager shortcutManager
= c.getSystemService(ShortcutManager.class);
if (shortcutManager.isRequestPinShortcutSupported()) {
Intent intent = new Intent(
c.getApplicationContext(), c.getClass());
intent.setAction(Intent.ACTION_MAIN);
ShortcutInfo pinShortcutInfo = new ShortcutInfo
.Builder(c,"pinned-shortcut")
.setIcon(
Icon.createWithResource(c, R.drawable.qmark)
)
.setIntent(intent)
.setShortLabel(c.getString(R.string.app_label))
.build();
Intent pinnedShortcutCallbackIntent = shortcutManager
.createShortcutResultIntent(pinShortcutInfo);
//Get notified when a shortcut is pinned successfully//
PendingIntent successCallback
= PendingIntent.getBroadcast(
c, 0
, pinnedShortcutCallbackIntent, 0
);
shortcutManager.requestPinShortcut(
pinShortcutInfo, successCallback.getIntentSender()
);
}
}
Run Code Online (Sandbox Code Playgroud)
正在为我工作。我知道 7.1 中有一些变化,不知道这是否适用于他们,我不知道上面提到的启动器问题。
这是在运行 Android 8.0.0 的三星 Galaxy Tab S3 上进行测试的。
我在你的github主页上放了一个简单的应用程序,它只为自己安装一个快捷方式。它适用于 Android 8 之前和之后的版本。Android 8 之前的版本使用 sendBroadcast 方法,之后创建固定快捷方式。
| 归档时间: |
|
| 查看次数: |
3702 次 |
| 最近记录: |