In my app I am creating a home screen shortcut for my app using following code:
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, "Handbook Manual");
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);
This is working fine and shortcut is getting created. But, rather than hardcoding the name of shortcut, I want to give it from string file like this:
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, R.string.app_name);
But after making this change shortcut is not getting created which is really wierd. I need this as my app supports multiple languages and even the name of app is based on language. I am testing on Samsung Galaxy S3.
Please help..
Here it is!
Have you tried using getString(R.string.app_name)? Remember that R.string.app_name is an integer, not a string.
Hi try to used this coding in the app
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
instead of this
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, R.string.app_name);
i hope this help u
Related
I am creating multiple shortcuts of an Activity in my application by using this code
Intent shortcutIntent = new Intent(getApplicationContext(), SingleChat.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bitmap bm = ((BitmapDrawable)contact_image.getDrawable()).getBitmap();
Bitmap bti = Bitmap.createScaledBitmap(bm, 120, 120, false);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra("user_id", csUserId);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, csUserName);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bti);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
Everything is working perfectly fine for shortcut creation but I want to read the data from the shortcut OR
I want to read this user_id whenever that shortcut is clicked.
I can't put it in SharedPreferences as it can be more than one shortcuts each linking to different user_id.
I have searched and so far couldn't find anything.
Shortcuts have been improved quite a lot after android 7.0 but I have to make it working for older devices too (say from 4 to onward).
Thanks.
Everything is working perfectly fine for shortcut creation
FWIW, I don't think it will work on Android O.
I want to read this user_id whenever that shortcut is clicked.
Put it as an extra in shortcutIntent.
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 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.
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);