Android Tutorial Add shortcuts to your Android application

Status
Not open for further replies.
New method to add shortcuts: https://www.b4x.com/android/forum/threads/support-for-app-shortcuts.86663/#post-548645
Updated instructions for this method:
https://www.b4x.com/android/forum/threads/solved-shortcut-example-not-working.105195/#post-659178

Android allows the user to add desktop shortcuts to any application.
Pressing on such a shortcut is exactly the same as starting your application from the applications list.

However it is also possible to create custom shortcuts which will have a custom behavior. For example start a different activity or start your application in a different mode.
In order to add a custom shortcut you should create a dedicated "shortcut Activity". This activity will be started when the user adds your shortcut from the shortcuts list. The activity should return an intent as the result which will later start the application.
The following screenshots demonstrate the shortcut behavior on a Honeycomb device. It should be similar to regular devices:

Shortcuts list:
shortcut_1.jpg


Desktop:
shortcut_2.png


Our application launched from the shortcut:
shortcut_3.png


You should edit the manifest file and set an intent filter to the shortcut activity. This tells the OS that your application includes a custom activity.
Please read the section about manual modification of the manifest file here.

This is the shortcut activity declaration in the manifest file of the example program:
B4X:
        <activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="shortcutactivity"
            android:label="Add Shortcut" android:screenOrientation="unspecified">
         <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
      </activity>
android:name is the lower cased name of the shortcut activity.
android:label is the title that will appear in the shortcuts list.

The code of the shortcut activity is:
B4X:
Sub Activity_Create(FirstTime As Boolean)
 Dim shortcutIntent As Intent
 shortcutIntent.Initialize("", "")
 shortcutIntent.SetComponent("anywheresoftware.b4a.samples.test/.main") '<--- change to match your package name / and target activity.
 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", "Shortcut Test")
 in.PutExtra("android.intent.extra.shortcut.ICON", LoadBitmap(File.DirAssets, "small_logo.png"))
 
 Activity.SetActivityResult(-1, in)
 Activity.Finish
End Sub
You should change the line with the comment to match your package name and target activity. Note that the syntax is: package_name/.activity and activity should be lower cased.

The last step that should be done is to check in our main activity whether our program was launched from the shortcut:
B4X:
Sub Activity_Resume
   Dim in As Intent
   in = Activity.GetStartingIntent
   Log(in)
   If in.HasExtra("from_shortcut") AND in.GetExtra("from_shortcut") = True Then
      Msgbox("Launched from shortcut", "")
   End If
End Sub
 

Attachments

  • Shortcut.zip
    9.5 KB · Views: 2,040
Last edited:

RiverRaid

Active Member
Licensed User
Longtime User
Strange Problem...

Hi.

I have a strange Problem...
my Mainfest:
B4X:
AddApplicationText(
<activity android:windowSoftInputMode="stateHidden" android:launchMode="singleTop" android:name="shortcutactivity" 
        android:label="ahaSmokefree" android:screenOrientation="unspecified">
    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>)

The Code in Activity "shortcutactivity":
B4X:
Sub Activity_Create(FirstTime As Boolean)
 Dim shortcutIntent As Intent
 shortcutIntent.Initialize("", "")
 shortcutIntent.SetComponent("com.ahasoft.rauchfreiBETA/.main") '<--- change to match your package name / and target activity.
 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", "Shortcut Test")
 In.PutExtra("android.intent.extra.shortcut.ICON", LoadBitmap(File.DirAssets, "applogo.png"))
 Activity.SetActivityResult(-1, In)
 Activity.Finish
End Sub

If I want to create the Shortcut in Android (The Text and Symbol is correctly displayed under Widgets in the App Drawer) than there comes a ToastMessage "App not installed".

The PackageName and Activity is spelled correct. Du you have any ideas?

Thanks!
 

RiverRaid

Active Member
Licensed User
Longtime User
Im Quinte sure, i copied it via strg-c and strg-v Form the tools-package menu. Quote strange...
 

jamessp

Member
Licensed User
Longtime User
Good morning, I passed by Eriel the procedure and failed here, I downloaded the example and still did not work!
What did I do wrong?
:sign0085:
 

hookshy

Well-Known Member
Licensed User
Longtime User
I think it will be interesting to make an example by creating a shortcut that offers you the opportunity to
open app developed only with b4A ?
In this matter il will be more easy to open developed app !
Thank you!
Can you mouve this request to wish list ?
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi Erel,

I am not able to Automatically create a shortcut once the application installs in my phone. I added the shortcut activity also Edited the manifest as per you mentioned.

Kindly let me know.

Regards,
SK
 

systems1

Member
Licensed User
Longtime User
I downloaded the Shortcut.zip. but when I run the app in my device it is not adding any shortcut to my homepage. and also please see the log
B4X:
LogCat connected to: B4A-Bridge: HTC HTC One S-352762055129851
--------- beginning of /dev/log/main
Connected to B4A-Bridge (Wifi)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Installing file.
PackageAdded: package:anywheresoftware.b4a.samples.test
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
(Intent) Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=anywheresoftware.b4a.samples.test/.main }
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
(Intent) Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=anywheresoftware.b4a.samples.test/.main }
** Activity (main) Pause, UserClosed = true **
(Intent) Intent { act=android.intent.action.MAIN flg=0x20000000 cmp=anywheresoftware.b4a.samples.test/.main }
no extras
Error starting intent: (Intent) Intent { act=android.intent.action.MAIN flg=0x30000000 cmp=anywheresoftware.b4a.samples.test/.main }
Installing file.
PackageAdded: package:anywheresoftware.b4a.samples.test
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
(Intent) Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=anywheresoftware.b4a.samples.test cmp=anywheresoftware.b4a.samples.test/.main }
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
(Intent) Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=anywheresoftware.b4a.samples.test/.main bnds=[275,674][400,816] }
** Activity (main) Pause, UserClosed = true **
PackageAdded: package:com.applicationcraft.appc879559867854baf8d3f24d59621f57e

Thanks and regards.
 

Nerdworld

Member
Licensed User
Longtime User
I had the same issue with "App is not installed." - but solved it!

Instead of using AddApplicationText simply use AddActivityText this way:

B4X:
AddActivityText(Shortcut,
        <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>)
 

b4auser1

Well-Known Member
Licensed User
Longtime User
Is it possible to obtain the value of android:label="Add Shortcut" in the Shortcutactivity ?
I create several shortcuts, but would prefer to have a single Shortcutactivity for all of them and need a label value to distinguish.
 

b4auser1

Well-Known Member
Licensed User
Longtime User
You can add any key / value you like to the intent and then read it with Activity.GetStartingIntent.
I do it in an auxilary activity, which is declared in the manifest.
But now I have to declare a separate auxilary activity for each Shortcut.
Then in each of these auxilary activities I add a key with an unique value.
I process the key/value in one usual activity, which is called from Shortcut to perform actions.
But how to reduce number of auxilary activities ?
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
I developed an application that download some apks and then install them into the device.
So, right now, I want to create a shortcut for every apk that I installed.

Is it possible?
 
Status
Not open for further replies.
Top