Android Question Shortcut Home Screen

kkkpe

Active Member
Licensed User
Longtime User
I tried in every way described in the examinations of the forum to create a shortcut of my application on a HOME S5mini, but to no avail. The compilation of the program without errors. Do you have an example functioning properly?
 

Attachments

  • Shortcut.zip
    445.8 KB · Views: 170

Roycefer

Well-Known Member
Licensed User
Longtime User
Are you saying you want to programmatically place a shortcut for your app on the user's home screen? Note that Google strongly discourages this behavior. This is the code I use. Pay attention to the comments.

B4X:
Sub PlaceShortcut
    Dim shortcutIntent As Intent
    shortcutIntent.Initialize("", "")
    shortcutIntent.SetComponent("package.name/.main")      'replace package.name with your app's package name. The "/.main" will cause Main to be launched
   
    Dim in As Intent
    in.Initialize("", "")
    in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
    in.PutExtra("android.intent.extra.shortcut.NAME", "App Name")     'Replace App Name with your app's name

    Dim pm As PackageManager
    Dim i As Bitmap
    i.InitializeMutable(88, 88)          'I found this resolution to work best. Feel free to experiment. 
    Dim c As Canvas
    c.Initialize2(i)
    Dim rect As Rect
    rect.Initialize(0,0,88,88)
    c.DrawDrawable(pm.GetApplicationIcon("package.name"), rect)    'Replace package.name with your app's package name
   
    in.PutExtra("android.intent.extra.shortcut.ICON", i)
   
    in.PutExtra("duplicate", False)              'This line will prevent duplicate shortcuts from being placed on the home screen

    in.Action = "com.android.launcher.action.INSTALL_SHORTCUT"

    pheune.SendBroadcastIntent(in)
End Sub
 
Upvote 0

kkkpe

Active Member
Licensed User
Longtime User
OK thanks.Now it works, in my project needed was the pheune.SendBroadcastIntent(in) and enable library iphone
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Are you saying you want to programmatically place a shortcut for your app on the user's home screen? Note that Google strongly discourages this behavior. This is the code I use. Pay attention to the comments.

B4X:
Sub PlaceShortcut
    Dim shortcutIntent As Intent
    shortcutIntent.Initialize("", "")
    shortcutIntent.SetComponent("package.name/.main")      'replace package.name with your app's package name. The "/.main" will cause Main to be launched
  
    Dim in As Intent
    in.Initialize("", "")
    in.PutExtra("android.intent.extra.shortcut.INTENT", shortcutIntent)
    in.PutExtra("android.intent.extra.shortcut.NAME", "App Name")     'Replace App Name with your app's name

    Dim pm As PackageManager
    Dim i As Bitmap
    i.InitializeMutable(88, 88)          'I found this resolution to work best. Feel free to experiment.
    Dim c As Canvas
    c.Initialize2(i)
    Dim rect As Rect
    rect.Initialize(0,0,88,88)
    c.DrawDrawable(pm.GetApplicationIcon("package.name"), rect)    'Replace package.name with your app's package name
  
    in.PutExtra("android.intent.extra.shortcut.ICON", i)
  
    in.PutExtra("duplicate", False)              'This line will prevent duplicate shortcuts from being placed on the home screen

    in.Action = "com.android.launcher.action.INSTALL_SHORTCUT"

    pheune.SendBroadcastIntent(in)
End Sub


Hi Roycefer,

I am trying to get my head around this but can't see where the graphic gets to be the icon. IE Where is Icon.png?


Regards Roger
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
DonManfred/Royefer

Sorry, my bad wording. Where in the code does it identify ShortCutIcon.png?
My purpose for asking is that I wish to have an option in an App where the user can change the Icon. Similar in function to the App "Icon Changer Free" which appears to create a shortcut.

This line appears to retrieve the Apps Icon, for use as the shortcut Icon. If this is so, I assume it should be changed to something along the lines of: File.DirRootExternal&"/ShortCutIcon.png" to get a users chosen graphic.

Additional: "my project needed was the pheune.SendBroadcastIntent(in) and enable library iphone" What is the correct library for pheune in B4A?

Regards Roger
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
If you follow the code, you'll see a Bitmap created, called i. This Bitmap is initialized as mutable. Then a Canvas is created to draw on i. The icon is drawn onto the Canvas (and hence the Bitmap). Then the Bitmap is put as an Extra into the Intent "in". This Intent is what is eventually broadcast. You can draw whatever icon you like onto the Canvas.

pheune is of type Phone, found in the Phone library.
 
Upvote 0
Top