When a user installs an Android app, a launcher icon is created in the apps menu. Many users I talk to expect that when they install an app, an icon should appear automatically on their home screen ("launch pad").
A lot of apps achieve this somehow. My preference would be to have a window appear on install asking the user "Do you want to add a shortcut?" If that's not possible, any code that auto-adds the shortcut will do.
Android gives a bunch of code here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.html
It is implied that adding this code (and the related xml) to your project will do the trick. But it does not have the effect I want. It seems the code provided is passive, and I need to trigger it somehow.
So my question is:
How do I trigger the installation of a shortcut, and how do I make sure it happens only once, preferably triggered by some kind of "app install" event?
PS:
A complicating factor is that I am building my app using PhoneGap, meaning the main activity is not "Activity" but "DroidGap".
Intent shortcutIntent = new Intent(getApplicationContext(), HomeScreen.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AIMS ICD");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.aims));
addIntent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
In the example, it returns the intent in setResult(...). I believe you need to run sendBroadcast(intent) to trigger installation of the shortcut.
The class DroidGap extends Activity so you can just add in the code from the link you provided to add a shortcut.
Related
I am using minSdkVersion = 16 & targetSdkVersion = 25 for my app, and using the following code to create shortcuts for different Activities in my app.
private void addShortcut() {
//Adding shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.mipmap.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
Now if the user removes any shortcut that belongs to my app from his Home Screen, I need to set a flag in my Preferences. So, if the user removes the shortcut I need to get notified in my app. How can this be done.
using the following code to create shortcuts for different Activities in my app
No, you are using that code to request shortcuts. The user's home screen may not support such requests. The user's home screen might ask the user for confirmation, and the user might decline to add the shortcut. And on Android 8.0+, that broadcast no longer has any effect.
if the user removes the shortcut I need to get notified in my app
There is nothing in the Android SDK for this, sorry. There is no requirement for home screens to let anyone know about changes in the mix of shortcuts created via the legacy com.android.launcher.action.INSTALL_SHORTCUT broadcast.
I have to create my android application shortcut and put it at a particular position on home screen.
I am able to successfully able to create the shortcut for the application by the following code-
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "customizeScreenLayout");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
What can I add to the above code so that I can place the shortcut at a particular position in the home screen(i.e., at a particular x,y coordinate)?
I'm pretty sure there's nothing you can add to designate a particular position. Looking at the sources, it appears that the algorithm for positioning new shortcuts is hard-wired into InstallShortcutReceiver.
Just out of curiousity, if there was a way to do what you wanted, what would you want to happen if a shortcut was already at the designated position? How would you handle different screen sizes, where the designated position might not exist on the home page?
Thanks for the prompt reply. The problem that I have posted here is just a part of a project where I want to make an application which checks all the installed apps on the system and create short-cuts for them. After that checks the x, y coordinates from the database where we need to position the apps and puts it over there.
Now suppose designating a particular position was possible, what I intended to do was-
1) Uninstall all the shortcuts
2) And then, install the shortcuts of different apps at the required positions.
Also, assumption here is that I am dealing with a fixed screen size and designated positions always exist.
Let me know if you find something or if you need additional information.
Do you know is there a programmatic way to create a web shortcut on the phone user's home screen?
What I want to do is:
When the phone user clicks a button in our Android application, the application will then place a website shortcut onto the phone user's home screen.
First you'll need to add a permission to your manifest.xml
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT">
</uses-permission>
You'll need to build an intent to view the webpage. Something like...
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.blablaba.com"));
You can test this by creating a little test app and doing startActivity(i); This should open the browser. Once you verified that the above intent is correct you should move on to the next step.
Now you'll need to actually install the shortcut.
Intent installer = new Intent();
installer.putExtra("android.intent.extra.shortcut.INTENT", i);
installer.putExtra("android.intent.extra.shortcut.NAME", "THE NAME OF SHORTCUT TO BE SHOWN");
installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", I THINK this is a bitmap); //can also be ignored too
installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(installer)
;
It's also possible some home screens don't accept this, but most do. So enjoy.
EDIT:
Icon can be set to the shortcut using:
installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon));
As an addition to the correct answer by #Mike-dg and #Gagan, instead of using
putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon))
which requires a ShortcutIconResource, you can use
putExtra("android.intent.extra.shortcut.ICON", Bitmap))
which lets you use any bitmap as the icon. This makes it easy to use a the favicon of the shortcut's website as the icon.
You can't place UI elements on the phone's home screen through an .apk. For the web shortcut - you can create a widget that opens the browser (with the specified URL) upon click.
I have an application on google play. Once user download and installs, google play use to create a home screen widgets automatically (google play->settings->Auto-add widgets is checked). Due to some technical reasons I want to avoid this creation.
Is it possible to avoid this auto creation of home screen widget programmatically(through manifest or with any other options)?
Any answers will be highly appreciated, thanks in advance.
Removing shortcut in Android uses an Intent (UNINSTALL_SHORTCUT) to
perform the task.
So first add this permission to your manifest:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
and in java code
private void removeShortcut() {
//Deleting shortcut for MainActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
So now you can call it on ur first launch (may be when ur splash screen is loading, if you have any). Now its upto you how you utilize this :)
For more info.. http://viralpatel.net/blogs/android-install-uninstall-shortcut-example/
https://gist.github.com/zeuxisoo/1008973
This question already has answers here:
How can I place app icon on launcher home screen?
(6 answers)
Closed 5 years ago.
I want to create my app's shortcut/launcher icon on the homescreen as soon as I install my app (even before I start it). Is that possible? How might I do that?
Since ICS, you can do like this:
public void createShortCut(){
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), EnterActivity.class));
sendBroadcast(shortcutintent);
}
Please also refer to the source code of launcher at: this link
Edit : If somebody would miss reading comment so adding following line.
This requires "com.android.launcher.permission.INSTALL_SHORTCUT" permission
You forgot to add permissions:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission
android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Great tutorial here:
http://androidforbegineers.blogspot.in/2014/01/android-home-screen-shortcut.html#!
NOTE: This answer is now wrong. See, for instance, Robin's answer for a way to do this.
As far as I know, an app cannot force itself onto the home screen. It gets added to the app list that the launcher app maintains, but the home screen is generally under user control. Giving apps the ability to clutter up the home screen would be an open invitation for abuse.
If you install your application, neither your application, nor any services or other processes are active! Android want's the user to make the first step in "opening" the application. You are not able to install a shortcut directly! Thats the answer. Notice: On most devices the launcher will create a shortcut for your application itself.
Can you install shortcuts in runtime after the user has somehow started your app at least once? : yes (see Robins answer).
Is there a workaround?
Maybe, but no good one.
Update, Another hint: If you have already an application on the device of the user. (For example if your second app that gets installed is the key for "going pro" or something), then you actually can start the second application from the first one, without the user has started the second app ever (and add a shortcut).
Create function to call intent of shortcut:
private void criarAtalho() {
Intent shortcutIntent = new Intent(getApplicationContext(), SplashScreen.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "nomeDaApp");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
}
write on OnCreat the call of function:
if(!getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).getBoolean("IS_ICON_CREATED", false)){
criarAtalho();
getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).edit().putBoolean("IS_ICON_CREATED", true).commit();
}
set the permissions on manifest:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
First, add permission for adding shortcut to manifest:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Then, call the function below.
public void createShortcut(){
Intent intentShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable appicon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, appicon);
intentShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
intentShortcut.putExtra("duplicate", false);
sendBroadcast(intentShortcut);
}