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);
Related
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);
}
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);
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);
I want to create a shortcut in an android app, it lead to another activity which is not launcher of the app.
To create the shortcut itself you need a specially crafted activity which must:
Be defined in your AndroidManifest.xml with an intent filter with the action android.intent.action.CREATE_SHORTCUT.
Return a result, an Intent, containing your actual shortcut. The shortcut itself is represented by another Intent.
This activity will then show up when you longpress your desktop and select "Shortcuts".
Of course the shortcut by itself is not much use, so you must add an intent filter to whatever activity you want to get triggered by the shortcut. The intent filter should match whatever Intent you chose for your shortcut.
I wrote a small how-to on the subject, it's got more details: http://www.kind-kristiansen.no/2010/android-adding-desktop-shortcut-support-to-your-app/
Do tell me if anything is unclear in that post, I'll try to clear it up.
I have developed one method below for creating the shortcut icon on android Homescreen. Just call it.
private void ShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.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, "Test");
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);
}
Don't forget to change your activity name, icon resource . Happy coding !!!
I would like to know if there is an option of setting an auto icon link of my application in the user's desktop, after installing it?
The only way I know to do it, is that the user could drag it manually to his desktop from the applications list. Is there any way of doing it automaticlly for the user(withouth his touch) ?
Thanks,
Moshic.
Please don't do that automatically!!!
Let the user choose wether or not he want your shortcut!
Here is the code you need:
//Create shortcutIntent here with the intent that will launch you app.
Intent shortcutIntent = (...)
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,sName);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
Don't forget an extra permission in the Manifest!
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />