How to add app's shortcut to the home screen - android

I know it's not documented and won't work on every device, but I see more and more apps placing their shortcuts on the home screen after they got installed.
Found bunch of code chunks how to do it but for me they don't fit together.
This is what I got for now.
Need a permission in the manifest.
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Create an Intent of activity that should be called. Ex (from cgeek):
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("com.example.androidapp", "SampleIntent");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Create shortcut itself
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(addIntent);
My questions is:
Where this code should go to make shortcut added after .apk installed? I tried this code in the launcher activity, it creates broken(another story) shortcut every time app starts.

As far as I know, that's an optional feature of the Market app, not of the apps themselves. By design an application does not receive a broadcast about itself being installed. If that codes works, the soonest you can execute it is the first time the user launches the app. That said:
Do. Not. Automatically. Create. App. Shortcuts.
Ever.
Don't usurp the user's UI design.

I agree with the currently accepted answer, that you should not do this by receiving a broadcast or at-install time. Don't do anything without user interaction or permission.
However, if you provide a button in your application, you would put this in the buttons OnClick handler. It would then add a shortcut when the user selects the "add shortcut" option.

This can be possible just add the below code to your main activity in oncreate method
Intent HomeScreenShortCut= new Intent(getApplicationContext(),
MainActivity.class);
HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
HomeScreenShortCut.putExtra("duplicate", false);
//shortcutIntent is added with addIntent
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AppName");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
Add add this permission to your manifest
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

I used this code to create 3 shortcuts on homepage:
Intent HomeScreenShortCut= new Intent(getApplicationContext(),
MainActivity.class);
HomeScreenShortCut.setAction(Intent.ACTION_MAIN);
HomeScreenShortCut.putExtra("duplicate", false);
//shortcutIntent is added with addIntent
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, HomeScreenShortCut);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AppName");
addIntent.putExtra(
Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(
getApplicationContext(),
R.drawable.ic_launcher
)
);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);

Related

Create shortcuts in Home screen android

This question may sound duplicate but there is no answer that i found is working:
I have gone through these question(s):
Android create shortcuts on the home screen
But the proposed solution is not working.
I have used the below solution which is working in API Level < 23
Intent shortcutIntent = new Intent(context,
LedgerDetailActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ledgerBean.getName());
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_action_comments));
addIntent.setAction("android.intent.action.CREATE_SHORTCUT");
addIntent.putExtra("duplicate", false);
context.sendBroadcast(addIntent);
Added Permission:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
But the above solution is not working in all devices and giving weird functionality sometimes like below:
In some devices like Samsung Edge (with Android N) shortcuts are not getting created
In my emulator with Android 7.1 (Android N) only one shortcut is getting created
Can someone please help me, There is no official documentation for this feature, Please don't confuse with App shortcuts introduced in Android N. I need shortcuts in home screen.
For adding shortcut first you have to add permission in Android:
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
and for above 23 API check for runtime permission for that use below link:
Runtime Permission in Android
Now add shortcut:
private void createShortcut() {
//Adding shortcut for SampleActivity
//on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),
SampleActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your App Name");
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);
}
If you find that multiple shortcuts created to avoid that you can check if the shortcut has been created or not:
if(!getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).getBoolean(Utils.IS_ICON_CREATED, false)){
addShortcut();
getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).edit().putBoolean(Utils.IS_ICON_CREATED, true);
}

Home screen shortcut to another application

I am making an app which makes a home screen shortcut for another app of mine if user has it installed.
It works partially. On API level less then 23 it works perfectly. On android 6 it creates the shortcut, but bypasses Intent.EXTRA_SHORTCUT_NAME and Intent.EXTRA_SHORTCUT_ICON_RESOURCE and leaves original icon and name, which I don't want to use.
Here is the code example I am using:
ApplicationInfo selectedApp; //app that should be used for shortcut
Intent shortcutIntent = new Intent(getPackageManager().getLaunchIntentForPackage(selectedApp.packageName));
shortcutIntent.setAction(Intent.ACTION_MAIN);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyNewShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic1));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
Manifest:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Is there anything different that I should do on Android Marshmallow?
EDIT
Ok, this is a bit confusing. I managed to make it work somehow.
When I add this line:
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "asd");
It creates a shortcut on the main screen, but with name that I set on addIntent, and not "asd" like on the new line. Is there any logic explanation for that?
It seems that there is some kind of a bug for Android M.
For shortcut to get new icon and name, I had to put extra name to the first intent too. And it could be an empty string because the name will stay from second intent. It is working like this now:
ApplicationInfo selectedApp; //app that should be used for shortcut
Intent shortcutIntent = new Intent(getPackageManager().getLaunchIntentForPackage(selectedApp.packageName));
shortcutIntent.setAction(Intent.ACTION_MAIN);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "asd"); //EDIT additional string for first intent that fixes Android M
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyNewShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic1));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);

How to add message app icon on home screen in custom launcher programatically?

How i can add application icons on home screen in my custom launcher. In my custom launcher only one button showing which is for open all applications window in grid view. But i want to add more application icons on home page.
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Using above permission You can write below code
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("packageName", "className");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "shortcut_name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(addIntent);

Android - adding shortcut to homescreen

When I use the firebox browser on my android device, I observe they have an option to bookmark a page such that it appears as shortcut (like an app) in the android launcher.
For example:
Here's before I open firefox:
In firefox I navigate to a page and then select the option at the bottom:
When I go back to the launcher, I can see a new icon. Clicking that icon allows me to go that page directly in firefox.
Does anyone know how this is achieved in the app ?
This can be done using the following:
Intent shortcutIntent = new Intent();
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutTitle);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, shortcutIcon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
MyActivity.this.sendBroadcast(shortcutIntent);
With the following permission set in the manifest:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
In your case of opening a specific URL, your target intent will contain the intent of the activity you wish to launch, and could also have some extras that would contain the desired URL.
try this,
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, R.string.app_name);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.app_icon));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);

Toast in shortcut creation in home in android

I am developing a app which will be distributed from my website. So when this app is installed the shortcut should be created in the home screen and i used the following code and it is creating shortcut icon when installed but with a toast message. I want to suppress this toast message. I am adding the function used to create shortcut home icon.
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.drawable.ic_launcher));
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
I know the question is already asked but no answer . so i gave the question in a different way with code and Image.
Looking the google's source of InstallShortcutReceiver it seems not possible (search for Toast).
From the research in android toasting , i have found that toast from the shorcut creation cannot be removed . If you are going through google store, then there is a option for creating shortcut icon in home screen. Then the toast will not come.

Categories

Resources