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.....
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.
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.
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:
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.