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,059
Last edited:

hackhack

Active Member
Licensed User
Longtime User
Astro can make a shortcut to say a picture, and that shortcut keeps working even after you uninstall astro.

Can this do that? You write for the 'setcomponent' to set it match "your package name" - if we instead set it to the system handler for pictures, would that launch the picture viewer instead?
 

hackhack

Active Member
Licensed User
Longtime User
I do not know how Astro shortcut mechanism works.

Well neither do I unless we disassemble it, but as I wrote elsewhere I'm assuming they just put in a shortcut intent to another app than their own.

The inner Intent will be sent when the shortcut is clicked. You can create any Intent that you like as the inner intent.

Once your application is uninstalled then the shortcut will be removed as well.

No it isn't. Not here anyway. its left behind when i uninstall the app.

If the intent refers to the app I'm told the app doesn't exist (as it should)


But if I do

B4X:
Dim in2 As Intent
Dim pm As PackageManager
in2 = m.GetApplicationIntent("com.google.android.youtube")

And pass in2 in in.PutExtra("android.intent.extra.shortcut.INTENT",in2) then the shortcut launches the youtube app, whether or not my app is installed.


The one thing which is annoying to me if you move the shortcut creation routine out to the main app and run it then it doesn't work - it has to be created on call initiated from Android shortcut menu (the Honeycomb "more") - can that be avoided?
 

splatt

Active Member
Licensed User
Longtime User
Start app from widget

Can this process be used to start an my application at a specific place if the user clicks on the apps widget?
 

splatt

Active Member
Licensed User
Longtime User
I'm developing an app that also has a widget, showing time, date and the next event due from a diary.

What I should have asked is if this is the preferred way to launch the underlying application at say the Diary screen, if the user clicks on the widget?
 

splatt

Active Member
Licensed User
Longtime User
I'm only just getting my head around widgets and services.

As I understand it the widget is created and maintained by a service. So if the user clicks on a label on the widget, how can I then launch the underlying application, which may or may not be running and start it at a specific screen.

I read in the help files that you shouldn't use StartApplication from a service, so what is the recommended way?

Thanks for any help.

EDIT: Answered my own question. I've added a global Boolean variable called 'StartedFromWidget' to the Service Module and set it to true in the Click event. I then start the app and test this variable and if it is true, show the required screen. It's still confusing when the Help says you should't use StartActivity, but the first widget example does just that|
 
Last edited:

rnunez81

New Member
Licensed User
Longtime User
Erel,

I got this example to run, it creates an activity that gets listed in the "Shortcuts" from the phone home screen (I mean: if I long press on any empty space of the home screen, I get a screen that says "Add to home screen" and I can chose to add a shortcut, widget, folder, wallpaper or theme. By doing what is indicated in your post, I get the application listed in the shortcuts that can be added to the home screen, but the shortcut is not automatically added to the home screen at the time of install (which is what I wanted to do).

Do you know how to do that extra step?

From information I've found around, it seems like shortcuts are added automatically to the home screen ONLY if the application is installed using Google Play AND ONLY if the user selected the "Auto-add shortcuts" option in Google Play's setting. This would mean that if I distribute my App via email among my co-workers (because my app is very focused to our business), then there is no way to have this shortcut added automatically at time of instalation. Is this so?

In other words, if the user can long click on the app icon from the App Drawer and drag it to the home screen to create a shortcut there... what benefit does this programming add? Please help me understand.

Thanks,

Roberto
 
Last edited:

JesseW

Active Member
Licensed User
Longtime User
Erel, can I programmaticly add a shortcut? It'd be nice if my B4Script app could place shortcuts on the homescreen to launch the app in run mode, executing a script :)

Jesse
 

JesseW

Active Member
Licensed User
Longtime User
It cannot be done without user intervention. You can create a widget (which the user will add to the home screen) and then automatically customize based on the available programs.

actually, both mintoris basic and rfo basic allow the user to create shortcuts with no system user intervention. x11-basic uses the widget approach you mention, and that would have been my last resort. thanks for the reply.

While I havent tried it, this thread here suggests you can make a shortcut from within the app.

thank you so much! I took a look at that thread, and it appears to be exactly what I was looking for! :D
 

Rusty

Well-Known Member
Licensed User
Longtime User
Erel,
In one of your posts, you indicate that you can add a shortcut to the user's home screen with permission.
Can you point me to an example where I can create a shortcut for my app on the home screen with permission (or without)?
I would like to have my program install and programatically add its shortcut to the user's home screen so they don't have to find it and drag/drop it there.
Thanks,
 

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel.
Does this mean that if I "sideload" applications it is not possible, only possible if an app is installed from Google Play?
How is this set in the Google Play settings?
Rusty
 
Status
Not open for further replies.
Top