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

Discussion in 'Tutorials & Examples' started by Woinowski, May 5, 2012.

  1. Woinowski

    Woinowski Active Member Licensed User

    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:
    Code:
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    Step 2: Create the Shortcut

    Code:
    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.basic4ppc.com/forum/basi...-cardboard-put-your-notes-cork.html#post90589), you need to clean that up for your needs

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

    Code:
    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:
     
  2. JohnC

    JohnC Well-Known Member Licensed User

    Very nice work - will come in handy!
     
  3. jamessp

    jamessp Member Licensed User

    Hello,
    I'm doing this procedure you passed by, but not this aperecendo no shortcuts!
    'm ustilizando 2.2 version of android
     
  4. Woinowski

    Woinowski Active Member Licensed User

    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.
     
  5. viljemto

    viljemto Member Licensed 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?

    Code:
    AddPermission(com.android.launcher.permission.UNINSTALL_SHORTCUT)
    Code:
    In.Action = "com.android.launcher.action.UNINSTALL_SHORTCUT"
     
  6. viljemto

    viljemto Member Licensed User

    Maybe any thought on uninstalling/removing shortcut icon? Maybe some permission missing?
     
  7. shashkiranr

    shashkiranr Active Member Licensed 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: Feb 4, 2014
  8. dibb3386

    dibb3386 Member Licensed User

    Thanks, this will save me alot of time
     
  9. marcick

    marcick Well-Known Member Licensed 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 ?
     
  10. Traiser

    Traiser Member Licensed User

    Hi,
    if i use another target activity, when i click the shortcut, this returns permission denied. how to resolve it?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the full error message from the logs?
     
  12. Traiser

    Traiser Member Licensed User

    the app don't generate errors in the logs, i use this code for create the shortcuts at home:
    Code:
    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:

    Code:
    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
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Add this line to the manifest editor and see whether it solves this issue:
    Code:
    SetActivityAttribute(popup, android:exported, true)
     
    Traiser likes this.
  14. Traiser

    Traiser Member Licensed User

    it works! thank you very much!
     
  15. Firpas

    Firpas Active Member Licensed 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??
     
  16. Firpas

    Firpas Active Member Licensed User

    Other cuestión ...

    How to delete ShortCut on unistall application ???
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    It should be uninstall automatically. If not then there is nothing that you can do about it as your program isn't notified that it is uninstalled.
     
  18. luciano deri

    luciano deri Active Member Licensed User

    Why icon create by my App whith this metod is smaller then the icon create manually?
     
  19. luciano deri

    luciano deri Active Member Licensed User

    Is possible install an Application without create the icon on application desktop? I need create some apps calls only by a Master App.
     
  20. zoujoo

    zoujoo Member Licensed 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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice