Android Tutorial How to create a shortcut DIRECTLY from within your application

I know there are some threads on this topic, and I had the feeling that there might be some misunderstandings. So to save you the time to read through all those threads and get the solution at once: Here is the way to automatically create a shortcut from within your application without further usr interaction. The user need not do anything in the launcher, the shortcut will just appear like magic :)

Step 1: Add a permission with the manifest editor. Put this into the section AddManifesText:
B4X:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

Step 2: Create the Shortcut

B4X:
Sub CreateEmptyShortCut
        Dim PrefMgr as PreferenceManager
   If PrefMgr.GetBoolean("shortcutinstalled") Then
      Return
   End If
   
   Dim shortcutIntent As Intent
   shortcutIntent.Initialize("", "")
   shortcutIntent.SetComponent("com.application.domain/.main") ' Put the app package name here
   
   Dim in As Intent
   in.Initialize("", "")
   in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
   in.PutExtra("android.intent.extra.shortcut.NAME", "Your App Name") ' Put you're application name here
        in.PutExtra("android.intent.extra.shortcut.ICON", LoadBitmap(File.DirAssets, "YourIcon.png")) ' If you have the icon file in the assets, you just need any bitmap here. Best is 72x72 pixels because launchers do not scale. You might want to experiment a little bit with size
   
   ' Here starts the real action that all the other posts had missing:
        ' How to get this OUT of your app to the launcher
        ' You need to send a broadcast, that's all :-)
   in.Action = "com.android.launcher.action.INSTALL_SHORTCUT"
   
   Dim p As Phone
   p.SendBroadcastIntent(in)
   DoEventsNow
   PrefMgr.SetBoolean("shortcutinstalled", True)
End Sub

3. If you want, you can add extras, just a sample from my app CardBoard (http://www.b4x.com/forum/basic4andr...-cardboard-put-your-notes-cork.html#post90589), you need to clean that up for your needs

B4X:
Sub CreateShortCut_Click
   If CurrentState = States.EMPTY OR DataManager.CurrentDBName = Null OR DataManager.CurrentDBName = "" Then
      Return
   End If
   
   Dim shortcutIntent As Intent
   shortcutIntent.Initialize("", "")
   shortcutIntent.SetComponent("de.woinowski.cardboard/.main")
   shortcutIntent.PutExtra("CurrentDBName", DataManager.CurrentDBName)
   shortcutIntent.PutExtra("CurrentDBPath", DataManager.CurrentDBPath)
   shortcutIntent.PutExtra("NamedShortCut", True)
   
   Dim in As Intent
   in.Initialize("", "")
   in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
   in.PutExtra("android.intent.extra.shortcut.NAME", DataManager.RemoveSuffix(DataManager.CurrentDBName))
   If GetApi > 10 Then
      in.PutExtra("android.intent.extra.shortcut.ICON", LoadBitmap(File.DirAssets, "CardBoardMini.png"))
   Else
      in.PutExtra("android.intent.extra.shortcut.ICON", LoadBitmap(File.DirAssets, "CardBoardMicro.png"))
   End If      
   in.Action = "com.android.launcher.action.INSTALL_SHORTCUT"
   
   Dim p As Phone
   p.SendBroadcastIntent(in)
   DoEventsNow
End Sub

4. And then catch this on resume, again from my app. That code only checks if you come from a shortcut, needs to be put into Activity_Resume. Start with any action you like after that.

B4X:
   Dim fromShortCut As Boolean : fromShortCut = False
   Dim in As Intent
   in = Activity.GetStartingIntent
   fromShortCut = in.HasExtra("NamedShortCut") AND in.HasExtra("CurrentDBPath") AND in.HasExtra("CurrentDBName") AND _
            in.GetExtra("CurrentDBPath") <> "" AND in.GetExtra("CurrentDBName") <> "" ' AND _
            ' (in.GetExtra("CurrentDBPath") <> DataManager.CurrentDBPath OR in.GetExtra("CurrentDBName") <> DataManager.CurrentDBName)

' Now you can act upon that information
    If fromShortCut then
         ...
    End If

For reference, here are some of the other threads about the topic:
 

JohnC

Expert
Licensed User
Longtime User
Very nice work - will come in handy!
 

jamessp

Member
Licensed User
Longtime User
Hello,
I'm doing this procedure you passed by, but not this aperecendo no shortcuts!
'm ustilizando 2.2 version of android
 

Woinowski

Active Member
Licensed User
Longtime User
Hello,
I'm doing this procedure you passed by, but not this aperecendo no shortcuts!
'm ustilizando 2.2 version of android

It worked on my old 2.2 phone, too, so it is not an android version issue.

Do you have any error messages? There is one API version check to choose the icon image file, maybe you have not changed the file names there?

And, you need the images for the icons in the assets directory, too.
 

viljemto

Member
Licensed User
Longtime User
This code is great! It works for me.

I am also doing part of removing shurtcuts. I got it to remove shurtcut that I have made with action.INSTALL_SHORTCUT, but if I want to remove shortcuts that I have made manually, it is not working.

Any idea maybe?

B4X:
AddPermission(com.android.launcher.permission.UNINSTALL_SHORTCUT)

B4X:
In.Action = "com.android.launcher.action.UNINSTALL_SHORTCUT"
 

viljemto

Member
Licensed User
Longtime User
Maybe any thought on uninstalling/removing shortcut icon? Maybe some permission missing?
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi Woinowski,

Where do you put the Sub CreateEmptyShortCut . Do you add it in the Activity_Create of the Main Activity.

Kindly Let me know.

Edit : I got it :) . Yes it creates a shortcut when you open your application for the first time.

But i need to create a shortcut when user installs the application. I know this is possible. But do you have an idea how it is done?

Regards,
SK
 
Last edited:

marcick

Well-Known Member
Licensed User
Longtime User
Hi,
I succesfully used this tutorial but have one problem: if there is no room on the home page to add the shortcut icon, I see a message and the icon is not created. How to detect this situation ?
 

Traiser

Member
Licensed User
Longtime User
Hi,
if i use another target activity, when i click the shortcut, this returns permission denied. how to resolve it?
 

Traiser

Member
Licensed User
Longtime User
the app don't generate errors in the logs, i use this code for create the shortcuts at home:
B4X:
Dim shortcutIntent As Intent
        shortcutIntent.Initialize("", "")
        shortcutIntent.SetComponent("b4a.example/.popup")  'this work only with main
        shortcutIntent.PutExtra("name", Value)
        shortcutIntent.PutExtra("from_shortcut", True)
     
        Dim In As Intent
        In.Initialize("", "")
        In.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
        In.PutExtra("android.intent.extra.shortcut.NAME", Value)
        In.PutExtra("android.intent.extra.shortcut.ICON", LoadBitmap(File.DirAssets, "icon.png"))
        In.Action = "com.android.launcher.action.INSTALL_SHORTCUT"

        Phone1.SendBroadcastIntent(In)
        Activity.Finish

and in the manifest i insert this:

B4X:
AddPermission(com.android.launcher.permission.INSTALL_SHORTCUT)

this code work only if i use the main activity, dont work with another activity, when i click on the shortcut appears toast with this message: permission denied
 

Firpas

Active Member
Licensed User
Longtime User
My ShortCut with icon of 72 x 72 pixels work fine on my Tablet Samsun Galaxy 10",

But in my Samsung Galary S4, The icon is not resiced why??
 

luciano deri

Active Member
Licensed User
Longtime User
Is possible install an Application without create the icon on application desktop? I need create some apps calls only by a Master App.
 

zoujoo

Member
Licensed User
Longtime User
In my Samsung Galary NOTE3, The icon is not resiced why?
ShortCut with icon of 72 x 72 pixels ,but shortCut is displayed only 1/4.
 
Top