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:
Step 2: Create the Shortcut
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
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.
For reference, here are some of the other threads about the topic:
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:
- http://www.b4x.com/forum/basic4andr...tcuts-your-android-application.html#post63900
- http://www.b4x.com/forum/basic4andr...ogrammatically-create-shortcut.html#post71926
- http://www.b4x.com/forum/basic4andr...ogrammatically-create-shortcut.html#post71926
- http://www.b4x.com/forum/basic4andr.../11406-can-we-make-shortcuts-3.html#post63981