I am trying to create a shortcut for my application through a service.
The following code is working on SDK <= 21, but its not working correctly on SDK = 23
The creation of the shortcut is done this way:
Intent shortcutIntent = new Intent(this, MainActivity.class);
shortcutIntent.putExtra(EXTRA_SHORTCUT_CLICKED, true); //This is only to check when the user clicked on the created shortcut
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
Intent addIntent = new Intent();
addIntent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TEST");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.application_icon));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
sendBroadcast(addIntent);
In SDK <= 21 the shortcut is created and if already exists it will not create other instance.
In SDK = 23 the shortcut will only be duplicated if I press the created shorcut and try to create the shortcut again, or If I reboot the device and then try to create the shortcut again.
I tried to uninstall the shortcut first but without success on SDK 23, as follows:
Intent shortcutIntent = new Intent(this, MainActivity.class);
shortcutIntent.putExtra(EXTRA_SHORTCUT_CLICKED, true); //This is only to check when the user clicked on the created shortcut
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
Intent addIntent = new Intent();
addIntent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TEST");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.application_icon));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
sendBroadcast(addIntent);
This is the service implementation:
public class MyService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
createShortcut(); //This is where I create the shortcut
return super.onStartCommand(intent,flags,startId);
}
}
Did Android 6 changed the shortcut creation politics? I cannot find anything on the documentation.
[UPDATE] I tried to make this shortcut creation by clicking on a button on a sample application and this still happens. The shorcut will duplicate once I click on the created shortcut and then try to create it again. Same thing happens rebooting the device.
[UPDATE2] I was looking on the grep code for the Launcher3 google application and I found that the shortcut installation calls the registerSessionCallback which depends on the calling thread. Might be an issue regarding these new changes?
For people who got stuck with the same issue:
Google removed support of Uninstall shortcut on Launcher3 and Google now Launcher on Android M.
Additionally, the duplication of shortcuts is an issue on the lastest Android M build (MRA58K) and its now on the development team hands.
You guys can follow the issue here, https://code.google.com/p/android/issues/detail?id=179697
[UPDATE] Google has just fixed the issue and it will be released on the next version.
Maybe this is helpful for some of you. While Google removed the support
for the uninstall of a shortcut, a shortcut, that points to an activity-alias
will still get removed, once the alias is disabled. ;)
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 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 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 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 !!!