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:
 

lemonisdead

Well-Known 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.
This comes from the launcher used and I never found a way to be sure of the behavior.
You could try to use a 48x48 or a 9patch image. But there are some ideas only and not 100% it will be good
 

zoujoo

Member
Licensed User
Longtime User
This comes from the launcher used and I never found a way to be sure of the behavior.
You could try to use a 48x48 or a 9patch image. But there are some ideas only and not 100% it will be good
Thanks,
9patch image is ok.
 

luisftv

Member
Licensed User
Longtime User
How would you go on making a shortcut on the Android TV (like the Sony 4K Android TV) with this code? Can it be used? If not, can you point me in the right direction?

Thanks.
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
I'm trying your code.
What's Sub "DoEventsNow"?
Is it possibile to create a shortcut, for another application?
 
Last edited:
Top