Android Question How to add "Home Screen" Shortcuts with a button?

Discussion in 'Android Questions' started by luisftv, Jul 7, 2015.

  1. luisftv

    luisftv Member Licensed User

    Hi,

    I want to install my app to my phone via USB or wifi (from my NAS) or sd card to my phone, tablet, or Sony Android TV, and I want my app to install a home screen icon when I press a button within the app. I am not installing the app from the Google Play store.

    Can some one please provide a working *.b4a sample? The code provided here in the forum has been copy/pasted from working apps but that doesn't show/mention which libraries to use or where exactly the code goes... There is even a thread (by Woinowski) that shows how to have a button add (manually) a shortcut to the home screen, but I cannot modify the code successfully. I'm using B4A v.5. The example provided by Erel only adds a shortcut to the Application drawer but it does not add it to the home screen.

    Woinowski example:

    Code:
    Sub CreateShortCut_Click 'I already have a button in the Designer for this 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

    I understand from the code above that he makes a shortcut that point to different states of his database.

    I only need the code to create the shortcut to my app when tapping on the button, an icon to run the app. That's all.

    In cleaning his code, in the Main Activity, I get to:

    Code:
    Sub CreateShortCut_Click 'I already have a button in the Designer for this Click
       Dim shortcutIntent As Intent
       shortcutIntent.Initialize(
    """")
       shortcutIntent.SetComponent(
    "de.woinowski.cardboard/.main"'<---- can I type myapp.apk/main here?  I don't get this part.
       shortcutIntent.PutExtra("NamedShortCut"True)
     
       
    Dim in As Intent
       
    in.Initialize("""")
       
    in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
       
    in.PutExtra("android.intent.extra.shortcut.ICON"LoadBitmap(File.DirAssets, "mypic.gif"))
       
    in.Action = "com.android.launcher.action.INSTALL_SHORTCUT"
     
       
    Dim p As Phone
       p.SendBroadcastIntent(
    in)
       DoEventsNow 
    '<------------this is red and is missing something???
    End Sub
    But the last line: DoEventsNow is red... what does it need? What libraries, what extra "Activities"?

    Also, instead of "de.woinosky.cardbaord/.main", can I type the name of my app which is "myapp.apk"? Or is it just "myapp"? Where does the "de.woinosky." come from?

    In my Main Activity I have:

    Code:
    Sub Globals
        
    Private CreateShortcut As Button
    End Sub
    Then on the Designer I have a simple button called CreateShortcut.

    In other words, this simple app has only one button. When you press that button it should create an app shortcut on the phone, tablet, or Android TV home screen. That's it...

    Can some one please be kind and tell me what I'm missing? I mean, besides programing skills.

    B4A is awesome, but at the other end is a huge lack for a great programming tutorial for those like me who have never done programming before... from a non-programming mind point of view.

    Thanks in advanced.
     
    Last edited: Jul 8, 2015
  2. sonicmayne

    sonicmayne Member Licensed User

    The "de.woinosky.cardbaord/.main" is the package name and activity name that Woinowski is launching from their shortcut. You replace it with your package name (e.g. myapp/main) to launch your main activity, remember to use your package name.

    I think DoEventsNow should be DoEvents
     
  3. luisftv

    luisftv Member Licensed User

    You were right, the "DoEvents" was correct.

    Now the button makes the shortcut on the home screen. The shortcut icon is there too, and it has the intended name. The problem now is that when I click/tap on the shortcut it gives the message "Application is not installed on your phone".

    Here is the code so far:

    On the Manifest...

    Code:
    'This code will be applied to the manifest file during compilation.
    'You do not need to modify it in most cases.
    'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136
    AddManifestText(
    <uses-sdk android:minSdkVersion=
    "4" android:targetSdkVersion="14"/>
    <uses-permission android:name=
    "com.android.launcher.permission.INSTALL_SHORTCUT"/>
    <supports-screens android:largeScreens=
    "true"
        android:normalScreens=
    "true"
        android:smallScreens=
    "true"
        android:anyDensity=
    "true"/>)
    SetApplicationAttribute(android:icon, 
    "@drawable/icon")
    SetApplicationAttribute(android:
    label"$LABEL$")
    'End of default text.
    On the Main activity...

    Code:
    Sub Process_Globals
    End Sub

    Sub Globals
        
    Private CreateShortcut As Button
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("myappmain")
    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
    End Sub

    Sub CreateShortCut_Click
      
           
    Dim shortcutIntent As Intent
           shortcutIntent.Initialize(
    """")
           
    'shortcutIntent.SetComponent("myapp/main")    '<------ with or without this line the shortcut still gets created
           'shortcutIntent.PutExtra("name", "my app")        '<------ with or without this line the shortcut still gets created
           'shortcutIntent.PutExtra("from_shortcut", True)  '<------ with or without this line the shortcut still gets created

           
    Dim in As Intent
           
    in.Initialize("""")
           
    in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
           
    in.PutExtra("android.intent.extra.shortcut.NAME""My App")
           
    in.PutExtra("android.intent.extra.shortcut.ICON"LoadBitmap(File.DirAssets, "color01.png"))
           
    in.Action = "com.android.launcher.action.INSTALL_SHORTCUT"

           
    Dim p As Phone
           p.SendBroadcastIntent(
    in)
           
    DoEvents

    End Sub
    In the Designer I have a button called "CreateShortcut" which is found in the Sub Globals.

    Adding or not adding the code suggested in other tutorials in "Sub Activity_Resume" does not affect/help it.

    How do I make the shortcut load/run the app once is on the home screen?
     
  4. sonicmayne

    sonicmayne Member Licensed User

    Is your package name really myapp?

    Code:
    shortcutIntent.SetComponent("myapp/main")
    should probably have a different package name, and be uncommented.
     
  5. luisftv

    luisftv Member Licensed User

    Yes... once it is compiled, it reads - myapp - in the objects folder of B4A project. I tried that line with or without comment, period, with or without that line entirely... The shortcut gets created, I even get the message 'Shortcut "My App" created', then you see the icon and title on the home screen. Try to tap on it and you get the message "Application is not installed on your phone". The shortcut gets created no matter where I test it, my phone, tablet, but it won't run...

    As of matter of fact, I included here the project so anyone can run it and hopefully find a solution.

    Thanks.

    p.s. I have a transparent purple square as the app and home icon, just for testing purposes...
     

    Attached Files:

    Last edited: Jul 8, 2015
    Daniel-White likes this.
  6. sonicmayne

    sonicmayne Member Licensed User

    I tested it, and as you said I got "Application is not installed on your phone", however I noticed that you had shortcutIntent.SetComponent("My.App/.Main")

    It should be shortcutIntent.SetComponent("My.App/.main")

    I made the change, recompiled and I can tap the shortcut and it will open the app.
     
    Daniel-White and luisftv like this.
  7. luisftv

    luisftv Member Licensed User

    That was it!!!!! Thank you sonicmayne.

    All I need to add now is the "If-Then-Else" so that it will not go on making home icons forever... maybe I'm pushing it, but, do you know how to do that?

    Thanks a million. After all the combinations I had tried, how could I have skipped that one??!! The evil is in the details indeed.
     
  8. sonicmayne

    sonicmayne Member Licensed User

    This works for me:
    Code:
    In.PutExtra("duplicate",False)
     
    luisftv likes this.
  9. luisftv

    luisftv Member Licensed User

    Thanks to you sonicmayne, the code is complete now... fully working.

    Here it is:


    The code in the Manifest...

    Code:
    'This code will be applied to the manifest file during compilation.
    'You do not need to modify it in most cases.
    'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136
    AddManifestText(
    <uses-sdk android:minSdkVersion=
    "4" android:targetSdkVersion="14"/>
    <uses-permission android:name=
    "com.android.launcher.permission.INSTALL_SHORTCUT"/>
    <supports-screens android:largeScreens=
    "true"
        android:normalScreens=
    "true"
        android:smallScreens=
    "true"
        android:anyDensity=
    "true"/>)
    'SetApplicationAttribute(android:hardwareAccelerated, "false")
    SetApplicationAttribute(android:icon, "@drawable/icon")
    SetApplicationAttribute(android:
    label"$LABEL$")
    'End of default text.


    The code in the Main Activity...

    Code:
    #Region  Project Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
        
    #ApplicationLabel: My App
        
    #VersionCode: 1
        
    #VersionName:
        
    #SupportedOrientations: unspecified
        
    #CanInstallToExternalStorage: False
        
    '#DebuggerForceStandardAssets: true
    #End Region

    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
    #End Region

    Sub Process_Globals
    End Sub

    Sub Globals
        
    Private CreateShortcut As Button
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("myappmain")
    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
    End Sub

    Sub CreateShortCut_Click

        
    Dim shortcutIntent As Intent
        shortcutIntent.Initialize(
    """")
        shortcutIntent.SetComponent(
    "My.App/.main"'<------change My.App for your own app file name here, like "My.Game", look into Tools, Build Configurations for your app's name

        
    Dim in As Intent
        
    in.Initialize("""")
        
    in.PutExtra("duplicate",False)
        
    in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
        
    in.PutExtra("android.intent.extra.shortcut.NAME""My App"'<-----Type here the name of the shortcut, "My App" for "Your App"
        in.PutExtra("android.intent.extra.shortcut.ICON"LoadBitmap(File.DirAssets, "color01.png")) '<----your app image here, "color01.png" for "yourpic.png"
        in.Action = "com.android.launcher.action.INSTALL_SHORTCUT"

        
    Dim p As Phone
        p.SendBroadcastIntent(
    in)
        
    DoEvents
     
    End Sub


    The libraries in use are "Core" and "Phone".

    In the Designer, I have a button called "CreateShortcut" and it is found in the Main activity, in the "Sub Globals" section as a button.

    Once the button is pressed, it will create a shortcut icon on the phone/tablet home screen. If the icon already exist, it will not be duplicated.

    Again, thanks a million sonicmayne... everybody owes it to you, specially me, for completing this code.

    I'm not sure if this code will work on older version of B4A. I am using v.5x

    According to this forum, a few years ago (as far as 2012), this was not possible... Oh I love all the new B4A updates/upgrades... so many more possibilities...

    I also attached here the working and finished project file.
     

    Attached Files:

    Last edited: Jul 8, 2015
    Daniel-White and sonicmayne like this.
  10. luisftv

    luisftv Member Licensed User

    I just discovered that on "old" android devices the code for creating a home screen shortcut works flawlessly.

    However, on newer devices, say Android 4.3 (kitkat, jellybeans, lolipop, etc) or newer, the shortcut will be created and when pressing again on the button to make the shortcut it will also detect that it has already been made... BUT, after closing/exiting the app and waiting for a minute (or forcing the app to stop with another app like Clean Master or even from the "Settings, Apps" control), then, it will not detect the already existing shortcut and it will create a new one. That second one will be detected and it will not be duplicate it. However, repeat the process of closing the app, waiting for a minute, then open the app again and tap the button to create a home screen shortcut and it will be made again... now there are 3 shortcuts, etc.

    Do you have any ideas sonicmayne? Does any one has an idea how to fix it? I tried the If-Then-Else, but I'm not so good at this... I'm learning as I go...

    I know is not the end of the world, is just a little tiny hiccup... but, it is not done until it is done right all the way.
     
  11. sonicmayne

    sonicmayne Member Licensed User

    The only thing I can think of is removing it, then creating a new one.

    The following code removes your shortcut:

    Code:
    Sub RemoveShortCut

        
    Dim shortcutIntent As Intent
        shortcutIntent.Initialize(
    """")
        shortcutIntent.SetComponent(
    "My.App/.main"'<------change My.App for your own app file name here, like "My.Game", look into Tools, Build Configurations for your app's name

        
    Dim In As Intent
        
    In.Initialize("""")
        
    In.PutExtra("duplicate",False)
        
    In.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
        
    In.PutExtra("android.intent.extra.shortcut.NAME""My App"'<-----Type here the name of the shortcut, "My App" for "Your App"
        In.PutExtra("android.intent.extra.shortcut.ICON"LoadBitmap(File.DirAssets, "color01.png")) '<----your app image here, "color01.png" for "yourpic.png"
        In.Action = "com.android.launcher.action.UNINSTALL_SHORTCUT"

        
    Dim p As Phone
        p.SendBroadcastIntent(
    In)
        
    DoEvents
    End Sub
    Your manifest will also need:
    Code:
    AddPermission(com.android.launcher.permission.UNINSTALL_SHORTCUT)
     
  12. luisftv

    luisftv Member Licensed User

    Removing and then creating the shortcut does not work.

    How should the Sub CreateShortCut_Click code be warped with - If Then Else - so that:

    IF the shortcut_already_Exist on Home Screen = True Then Stop Sub
    Else If Shortcut_already_Exist on Home Screen = False Then Do Sub bla bla bla
    End If

    or something like that...?

    Any one?

    Here is again the code that I'm trying to wrap with If-Then-Else:

    Code:
    Sub CreateShortCut_Click

        
    Dim shortcutIntent As Intent
        shortcutIntent.Initialize(
    """")
        shortcutIntent.SetComponent(
    "My.App/.main"'<------change My.App for your own app file name here, like "My.Game", look into Tools, Build Configurations for your app's name

        
    Dim in As Intent
        
    in.Initialize("""")
        
    in.PutExtra("duplicate",False)
        
    in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
        
    in.PutExtra("android.intent.extra.shortcut.NAME""My App"'<-----Type here the name of the shortcut, "My App" for "Your App"
        in.PutExtra("android.intent.extra.shortcut.ICON"LoadBitmap(File.DirAssets, "color01.png")) '<----your app image here, "color01.png" for "yourpic.png"
        in.Action = "com.android.launcher.action.INSTALL_SHORTCUT"

        
    Dim p As Phone
        p.SendBroadcastIntent(
    in)
        
    DoEvents
      
    End Sub
     
  13. sonicmayne

    sonicmayne Member Licensed User

    When you say it doesn't work, what happens?
     
  14. luisftv

    luisftv Member Licensed User

    The same as if you don't add the code to delete the shortcut and then re-create it.

    After I press the button, the shortcut gets created on the home screen. If immediately I press the button to create the shortcut again, then it will tell me that the shortcut already exist. Exit the application, open it again, hit the create shortcut button, and it will also tell you that the shortcut already exist.

    BUT, if now you exit the application and terminate all apps on the phone and say clean the trash (using an app like Clean Master, for example), etc, then you wait about a minute, then open again the MyApp (using the shortcut on the home screen or not, makes no difference) and tap the create shortcut button, it will make a shortcut as if the first one does not exist. Tap the button again and it will tell you that the shortcut already exist... and here we go on a loop again. Turn of the device, turn it back on, open the app and tap the button to create the shortcut and it will make a new shortcut, again, and not notice that there are already two other shortcuts on the home screen.

    The app only "remembers" for a while that the shortcut has been made. If you force the app out of the device memory or reboot the device (phone/tablet) then the app will act as if the shortcut has never been made and create a new one, and it will keep going until it runs out of empty space.

    There has to be a way for the app to physically check that the shortcut already exist on the home screen, and if so, to stop the "Sub" action of the CreateShortCut button.

    NOTE: When using older versions of Android such as v.2.2 it works flawlessly all the time. If using newer version of Android like 4.3 or newer, then the problem show up. ONLY with Android OS v.4.x or newer.
     
    Last edited: Jul 12, 2015
  15. sonicmayne

    sonicmayne Member Licensed User

    Sorry, my mistake, I was referring to what happens if you remove it, then add it?
     
Loading...