Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Add shortcuts to your Android application

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-12-2011, 07:43 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Add shortcuts to your Android application

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:


Desktop:


Our application launched from the shortcut:


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:
Code:
        <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:
Code:
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:
Code:
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
Attached Files
File Type: zip Shortcut.zip (9.5 KB, 113 views)
Reply With Quote
  #2 (permalink)  
Old 09-12-2011, 12:43 PM
Basic4ppc Veteran
 
Join Date: Jun 2011
Posts: 298
Default

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?
Reply With Quote
  #3 (permalink)  
Old 09-12-2011, 12:49 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

I do not know how Astro shortcut mechanism works. 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.
Reply With Quote
  #4 (permalink)  
Old 09-12-2011, 03:23 PM
Basic4ppc Veteran
 
Join Date: Jun 2011
Posts: 298
Default

Quote:
Originally Posted by Erel View Post
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.

Quote:
Originally Posted by Erel View Post
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

Code:
    
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?
Reply With Quote
  #5 (permalink)  
Old 09-12-2011, 03:48 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
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?
Not that I know of.
Reply With Quote
  #6 (permalink)  
Old 01-30-2012, 07:53 PM
Senior Member
 
Join Date: Nov 2007
Location: Chorley, Lancs, UK
Posts: 152
Default 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?
__________________
Steve
Samsung Galaxy S(2.2) / Samsung Galaxy Tab 7" / HTC Touch Diamond 2 / HTC Exec

SpringfieldSoftware
Reply With Quote
  #7 (permalink)  
Old 01-31-2012, 05:42 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Pressing on the shortcut will start the chosen activity.
Reply With Quote
  #8 (permalink)  
Old 01-31-2012, 07:50 AM
Senior Member
 
Join Date: Nov 2007
Location: Chorley, Lancs, UK
Posts: 152
Default

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?
__________________
Steve
Samsung Galaxy S(2.2) / Samsung Galaxy Tab 7" / HTC Touch Diamond 2 / HTC Exec

SpringfieldSoftware
Reply With Quote
  #9 (permalink)  
Old 01-31-2012, 07:56 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

This is a different approach. If you are using a home screen widget then you should not use this method. I think that a widget is preferable in most cases.
Reply With Quote
  #10 (permalink)  
Old 01-31-2012, 08:26 AM
Senior Member
 
Join Date: Nov 2007
Location: Chorley, Lancs, UK
Posts: 152
Default

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|
__________________
Steve
Samsung Galaxy S(2.2) / Samsung Galaxy Tab 7" / HTC Touch Diamond 2 / HTC Exec

SpringfieldSoftware

Last edited by splatt : 01-31-2012 at 08:53 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Protect your Android application with the Licensing library Erel Basic4android Getting started & Tutorials 76 04-22-2012 01:07 PM
Signing your application before uploading to Android market Erel Basic4android Getting started & Tutorials 38 04-03-2012 01:38 PM
Is it possible trap an event from an Android application? TDowdeswell Basic4android Updates and Questions 12 09-10-2011 11:11 AM
accessing my database in my Android application juanM1 Libraries developers questions 8 05-31-2011 09:51 PM
Android Screenshot Application alfcen Chit Chat 2 01-11-2011 04:26 AM


All times are GMT. The time now is 10:00 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0