Can we make shortcuts?

hackhack

Active Member
Licensed User
Longtime User
On a PC if I have created a file, say a word document - I can right click on it and select "create shortcut" and then place that shortcut somewhere else, say on the desktop. If I later click on the shortcut of the document it launches the program set up to open word documents and loads the document.

I'd like to make that kind of shortcuts on the Android.

Now on the Android, if i long press on my Desire I get a popup menu with the items Widget/App/Shortcut/Folder - there are somewhat similar menus on different devices, but they probably all have "App" and "Shortcut" as two of the menu choices. The "App" choice adds what I would call a shortcut to an app (only) on the Android 'desktop' and "shortcut" is something else - shortcuts seem to be somewhat related to a widget, but isn't a widget (confusing much Google?)

NOW - if you download the Astro file manager from the market, and go to the above mentioned menu item called "Shortcut" (in Honeycomb it is called "more") Astro starts up and lets you find any file on the Android device and create a shortcut. You could select a pdf or sound or picture etc. You then get a shortcut on the desktop. If its a shortcut to a picture and you click it and the default picture viewer will launch and show the picture.

NOW if you uninstall the Astro filemanager the shortcuts remain on the desktop AND THEY STILL WORK

Meaning Android supports in some way.

So my question is can we in Basic 4 Android program this ability?
 
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
Intents.
But then there are so many different launchers out there, I do not know if they all have the same intents. You will have to program for stock launcher, adw, launcher pro, go launcher etc etc.

The handling picture is slightly easier. That also needs an intent in which you set the mime-type and android should launch a menu with all apps that can handle images (like gallery etc)
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
If you want to make a simple shortcut to you app (this is how i understand it), then hold your finger on the app in your phone, then you will be able to drag it on the desktop :confused:

Looking at what I wrote, I see that I was probably too tired when i typed it - but clearly I'm not going to ask about adding a shortcut to an app from a user point of view in a programming forum ,)

I'll edit to rephrase it.
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Intents.
But then there are so many different launchers out there, I do not know if they all have the same intents. You will have to program for stock launcher, adw, launcher pro, go launcher etc etc.

If you go up and read my edit about the Astro filemanager, are you suggesting it generates some sort of data file which is different based on what system it runs on?

I would guess it doesn't.

The handling picture is slightly easier. That also needs an intent in which you set the mime-type and android should launch a menu with all apps that can handle images (like gallery etc)

Hm, i'm not so sure - what intents did you have in mind?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
If you go up and read my edit about the Astro filemanager, are you suggesting it generates some sort of data file which is different based on what system it runs on?

I would guess it doesn't.

Yes, that is what I am suggesting. When you create a shortcut on ADW is that also transfered to the stock launcher? They do not share a common data file.
You need to know which launcher a user is using in order to kick off an intent there.
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Yes, that is what I am suggesting. When you create a shortcut on ADW is that also transfered to the stock launcher?

I don't understand the question.

They do not share a common data file.
You need to know which launcher a user is using in order to kick off an intent there.

Well I'm going to hope that is not the case since that would be stupid design.

Reading elsewhere it seems there is a database file which keeps information on what is visible on the desktop.

This would also mean launcher is irrelevant since any launcher should be able to handle standard icons, and these data file shortcuts are apparently an android standard (even if there is user way of creating them at the moment)
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Man, English is hard :-/

Anyway,

Androids intent seems to be android.intent.action.CREATE_SHORTCUT

I'm guessing when you create a shortcut it will trigger an intent when you press it. That intent might be a custom one for a custom launcher, but i would also guess there are a lot of standard intents than the normal launcher would catch (and any launcher i guess)

There seems to be some sort of demonstration for some of it here, but since I don't get Java I'm not sure exactly whats going on Android: Adding desktop shortcut support to your app » Roger Kind Kristiansen

But as far as the search engine is concerned, this will be the first time android.intent.action.CREATE_SHORTCUT is mentioned on the basic4ppc website.
 
Last edited:
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
I wonder if you need filter support when you just want to leave an icon behind and not intercept it (which is my first goal)

I also wonder if the Reflector thingy can be used for this :)
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
We need to have an intent listener for: android.intent.action.CREATE_SHORTCUT

So when the user long presses the desktop, he gets an option to add shortcut from your app.

They need to be specified in the manifest xml file.
An intent filter is an instance of the IntentFilter class. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.xml) as <intent-filter> elements. (The one exception would be filters for broadcast receivers that are registered dynamically by calling Context.registerReceiver(); they are directly created as IntentFilter objects.)
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
We need to have an intent listener for: android.intent.action.CREATE_SHORTCUT

So when the user long presses the desktop, he gets an option to add shortcut from your app.

Well for now I'd just settle for generating an icon which will work without me having installed any app (as i explained works with Astro)

But I guess we need higher level wizards to comment on this :)
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I tried this.

Add this to your manifest:
B4X:
<intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
After the intent filter.

Then from your launcher long press and go into Shortcuts.
It will have an icon for your app.

EDIT:

Also, look at this code and translate it to b4a
B4X:
void saveShortcut(Site site) {
       Intent shortcutIntent = new Intent(this, TabWidgetActivity.class);
       shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       shortcutIntent.putExtra("org.jtb.csdroid.site.id", site.getId());
    
       Intent intent = new Intent();
       intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
       intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, site.getName());
       intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
       intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
       setResult(RESULT_OK, intent);
       finish();
   }

From: Android Shortcuts « Zero Credibility
 
Last edited:
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
I tried this.

Add this to your manifest:
B4X:
<intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
After the intent filter.

Then from your launcher long press and go into Shortcuts.
It will have an icon for your app.

Ah yes, nice - but currently there is no functional difference between that and just adding a shortcut to the desktop (annoying both things are called shortcuts, is there a better word)






I tried this.
EDIT:

Also, look at this code and translate it to b4a
B4X:
void saveShortcut(Site site) {
       Intent shortcutIntent = new Intent(this, TabWidgetActivity.class);
       shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       shortcutIntent.putExtra("org.jtb.csdroid.site.id", site.getId());
    
       Intent intent = new Intent();
       intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
       intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, site.getName());
       intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
       intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
       setResult(RESULT_OK, intent);
       finish();
   }

From: Android Shortcuts « Zero Credibility

Yeah, if I knew how to do that I wouldn't be here ;)
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I have tried awhile playing with the following code:
B4X:
Sub saveShortcut
  Dim i1 As Intent 
  i1.Initialize(i1.ACTION_MAIN, "anywheresoftware.b4a.samples.sudoku")
  
  
Dim i2 As Intent 
i2.Initialize(i1.ACTION_MAIN, "")
'"com.android.launcher.action.INSTALL_SHORTCUT")
i2.AddCategory("DEFAULT")
i2.PutExtra("android.intent.extra.shortcut.INTENT" , i1)
i2.putExtra("android.intent.extra.shortcut.NAME" , "hello")
'i2.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
i2.Action="com.android.launcher.action.INSTALL_SHORTCUT"
StartActivity(i2)
End Sub

And it doesnt work. It says 'No Activity found to handle INSTALL_SHORTCUT"
so there...if the launcher wont handle this then I dont know!

I'm starting to think this is not even possible to do programmatically.
 
Last edited:
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Seems b4a only supports the intents ACTION_CALL, ACTION_EDIT, ACTION_MAIN, ACTION_PICK, ACTION_SEND and ACTION_VIEW and not the create shortcut intent. So unless you can do it with the reflection library for this we may not be able to do it now (i don't understand java, so i don't understand what the reflection library does)

I noticed you can't edit "main.java" and then write protected it (like you can with the manifest) the Ide will complain if the file is write protected.

The cool thing would be if a future version of b4a would have a MakeShortCut(IntentToIssueOnClick,Name,Icon) function.
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Look how close I was!!!

Thanks Erel. Everytime someone comes up with 'b4a doesnt support this yet' you come up with a whole bunch of tutorials and examples!!! Where do you get the time? :)

I have some more questions however:
B4X:
in.Initialize("", "")
When you initialise an intent like this, you dont define whether is a MAIN, VIEW etc intent. So there is no ACTION. What would this mean?

Nor do we tell it the package name (so this is a broadcast intent?).
Any launchers receiving this intent will add a shortcut? So if you have the stock and ADW launcher you will end up with a shortcut on both?

B4X:
Activity.SetActivityResult(-1, in)
What does this line do? you never started the intent?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
We are using SetComponent here. SetComponent explicitly defines the application and activity that will handle this intent. Therefore the action and Uri are less important here.

The launcher calls our shortcut activity and then takes the result which is an intent. By calling Activity.SetActivityResult(-1, in) we are setting the result intent. -1 is the value of RESULT_OK.
It will only affect the the launcher that started our shortcut activity.
 
Upvote 0
Top