Android 在主屏幕上创建快捷方式
我想做的是:
1)我在一个活动中,有 2 个按钮。如果我单击第一个,则会在主屏幕中创建快捷方式。该快捷方式打开先前下载的 html
页面,因此我希望它使用默认浏览器,但我不想使用互联网,因为我已经拥有该页面。
2)第二个按钮创建另一个启动活动的快捷方式。我想向活动传递一些额外的参数(例如作为字符串)......
这些事情可能吗?我发现了一些链接和一些类似的问题,例如 Android:有没有一种编程方法可以在主屏幕上创建 Web 快捷方式
它们似乎是我问题的答案,但有人告诉我这段代码并不适用于所有设备,那就是已弃用,这就是我想做的是不可能的......
不建议使用此技术。这是一个内部实现,不是 Android SDK 的一部分。它不适用于所有主屏幕实现。它可能不适用于所有过去版本的 Android。它可能无法在 Android 的未来版本中运行,因为 Google 没有义务维护内部未记录的接口。请不要使用这个
内部实现意味着什么?该代码是否可信......请帮助我......
What I want to do is:
1) I'm inside an activity, there are 2 buttons. If I click the first one a shortcut is created in my home screen. The shortcut open an html
page that has been previously downloaded, so I want it to use the default browser but I don't want to use internet cause I already have the page.
2)The second button create another shortcut that starts an activity. And i want to pass to the activity some extra arguments (As strings for example)...........
Are those things possible? I found some link and some similar questions like Android: Is there a programming way to create a web shortcut on home screen
They seem to be the answer to my question but someone told me that this code is not gonna work on all devices and that is deprecated and that what i want to do is not possible.......
This technique is not recommended. This is an internal implementation, not part of the Android SDK. It will not work on all home screen implementations. It may not work on all past versions of Android. It may not work in future versions of Android, as Google is not obligated to maintain internal undocumented interfaces. Please do not use this
What means internal implementation? Is that code trustable or not.....help me pls.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
示例代码使用未记录的接口(权限和意图)来安装快捷方式。正如“某人”告诉您的那样,这可能不适用于所有手机,并且可能会在未来的 Android 版本中出现故障。不要这样做。
正确的方法是监听来自主屏幕的快捷方式请求 - 使用清单中的意图过滤器:
然后在接收意图的活动中,为快捷方式创建一个意图并将其作为活动结果返回。
The example code uses undocumented interfaces (permission and intent) to install a shortcut. As "someone" told you, this may not work on all phones, and may break in future Android releases. Don't do it.
The correct way is to listen for a shortcut request from the home screen-- with an intent filter like so in your manifest:
Then in the activity that receives the intent, you create an intent for your shortcut and return it as the activity result.
我开发了以下一种方法,用于在 Android 主屏幕上创建快捷方式图标 [在我自己的应用程序上测试]。只要调用它即可。
不要忘记更改您的活动名称、图标资源和活动名称。允许。
快乐编码!
编辑:
对于重复问题,第一个选项是在代码中添加以下行,否则每次都会创建新的问题。
第二个选项是首先卸载应用程序快捷方式图标,然后如果第一个选项不起作用,则再次安装。
I have developed one method below for creating the shortcut icon on android Homescreen [Tested on my own app]. Just call it.
Don't forget to change your activity name, icon resource & permission.
Happy coding !!!
Edit:
For Duplicate Issue, First Option is to add below line in the code otherwise it creates new one every time.
Second Option is to First uninstall The App Shortcut Icon and then Install It Again If the First Option Didn't Work.
自 android oreo 以来,com.android.launcher.action.INSTALL_SHORTCUT 广播不再有任何效果。 LINK
如果您想支持所有 Android 版本,尤其是android 8.0 或 oreo 及更新版本,使用以下代码创建快捷方式:
在清单文件中添加权限:
The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect since android oreo. LINK
If you want to support all android versions, especially android 8.0 or oreo and newer, use the code below to create shortcut:
Add permission in manifest file:
从Android O开始,这是创建快捷方式的方式:
它有很多限制,遗憾的是:
对于 Android O 之前的版本,您也可以使用 ShortcutManagerCompat 创建新的快捷方式,而没有任何这些限制。
Starting from Android O, this is the way to create a shortcut:
It has a lot of limitations, sadly:
For pre-Android O, you can use ShortcutManagerCompat to create new shortcuts too, without any of those limitations.
我对上面的解决方案做了一点改进。现在,它会在首选项中保存是否已添加快捷方式,并且如果用户删除了快捷方式,则不会将其添加到应用程序的新启动中。这也节省了一点时间,因为添加现有快捷方式的代码不再运行。
I improved a little bit a solution above. Now it saves in preferences whether a shortcut was already added and doesn't add it in new launches of an app if user deleted it. This also saves a little bit time, since the code to add an existing shortcut doesn't run anymore.
@Siddiq Abu Bakkar 答案有效。但为了防止每次应用程序启动时创建快捷方式,请使用共享首选项。
@Siddiq Abu Bakkar Answer works. But in order to prevent creating shortcut every time app launches use shared Preferences.
自
API 级别 26
起,不推荐使用com.android.launcher.action.INSTALL_SHORTCUT
。创建快捷方式的新方法是使用ShortcutManager
。它提到有 3 种快捷方式:静态、动态和固定。
根据您的要求,您可以选择创建它们。
Since
API level 26
, usingcom.android.launcher.action.INSTALL_SHORTCUT
is deprecated. The new way of creating shortcuts are usingShortcutManager
.It mentions that there are 3 kinds of shortcuts, static, dynamic and pinned.
Based on your requirements, you can choose to create them.
要添加主屏幕快捷方式,请使用此代码。
并且不需要额外的许可!
To add a shortcut to the home screen use this code.
and no extra permission need !!!
我用过它,但有些设备的主屏幕上添加了 2 个图标。我没听懂??
I've used it, but some devices on the home screen adds 2 icons. I did not understand??