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.
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 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);
This question already has answers here:
Android create shortcuts on the home screen
(10 answers)
Closed 9 years ago.
This issue has arisen when I was developing an android application. I thought of sharing the knowledge I gathered during my development.
Android provide us an intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut.
First we need to add permission INSTALL_SHORTCUT to android manifest xml.
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
The addShortcut() method creates a new shortcut on Home screen.
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");
addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate
getApplicationContext().sendBroadcast(addIntent);
}
Note how we create shortcutIntent object which holds our target activity. This intent object is added into another intent as EXTRA_SHORTCUT_INTENT.
Finally we broadcast the new intent. This adds a shortcut with name mentioned as
EXTRA_SHORTCUT_NAME and icon defined by EXTRA_SHORTCUT_ICON_RESOURCE.
Also put this code to avoid multiple shortcuts :
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 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 !!!