I need to add a shortcut for my app on the home screen (programmatically).
I know that the app store do this by default, but for start, the app won't be on the google app store.
I searched a lot, and found basically the same lines of code over and over, and it doesn't seem to work for me.
the code I used:
in the manifest:
<activity android:name=".MainScreenActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
in the onCreate method I called the function that does the following:
private boolean createShortcut()
{
//create shortcut intent
Intent shortcutIntent = new Intent(getApplicationContext(),MainScreenActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
//create intent to add and define the shortcut
Intent addingIntent = new Intent();
addingIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
addingIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"SenseGuard");
addingIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.peak_detection_icon));
addingIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addingIntent);
}
I tried switching "getApplicationContext()" to "this".
I tried with an actual tablet and on an emulator but I can't get it to work.
Do like This:
Step 1:
Update your manifest.xml :
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Step 2:
in your MainActivity.java create addShortcut() method and in it`s block put this code :
private void addShourcut(){
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 , "Convertor");
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);
getApplicationContext().sendBroadcast(addIntent);
}
Step3:
set onClickListener for your view that be create shortcut :
img_pin = (ImageView) findViewById(R.id.img_pin);
img_pin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addShourcut();
Toast.makeText(MainActivity.this, "shortcut created !", Toast.LENGTH_SHORT).show();
}
});
This is worked for me ...
happy codinngggg...:)
That code isn't guaranteed to work. That broadcast is also sent by ShortcutManagerCompat (which you should probably be using instead of manually sending the broadcast).
However, there are two problems with this.
Your default launcher isn't guaranteed to listen for this broadcast. Nova Launcher, for example, has this behavior disabled by default. Other launchers might not even listen for that action at all.
On Android Oreo (26) and above, this won't work how you expect it to (read the comments on the method I linked for more details).
You can use this logic still and hope that it works for some of your users, but keep in mind that many default launchers no longer even have app drawers, so adding a shortcut could give your users duplicate icons. Also, I know that, at least for me, I have my home screen organized how I want, and if I install an app, it would be really annoying for it to add itself to my home screen.
If you are using the default AOSP launcher (or a close fork), however, and it isn't working, make sure you add this to your manifest:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Related
I uninstall an app programatically in Android, using an Intent, like you see below:
Uri packageURI = Uri.parse("package:"+packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
Which redirects to a prompt that asks if you want to uninstall the app.
Usually after that you can see a toast in the bottom of the screen saying the app was uninstalled. But I want to be able to be notified so I can remove an uninstall button in a view.
How can I know in the code when the uninstall of the package has been completed? Or if an error occurred? Or even, if user clicked "ok" to uninstall or "cancel" if he changed his mind, how can I know?
Is it possible to know any of this? Is there an alternative way to uninstall a package (without being a system app) and be notified?
Thank you for reading. Lemme know if you need any more information.
Well I ended up finding a solution, when a package is removed there is an intent that can be picked up by a receiver.
In my AndroidManifest
<application
<!--...-->
<receiver
android:name=".UninstalledBroadcastReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
Create a UninstalledBroadcastReceiver class that extends a normal BroadcastReceiver
public class UninstalledBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// handle the intent here
}
}
You can check for instance if your package name of the app that was installed matches your own and do something with it. In my case, I put some static fields in the UninstalledBroadcastReceiver, including an interface so that I could perform some callbacks. Don't think you can pass fields in the constructor, since the object is created when the intent is received.
I am trying to create a new application ("My New App") from the source of another application that I have ("My Previous App").
In order to do it, I've created a new flavor, changed the app name and new icons for the new flavor, and everything goes ok.
But I've encountered that I can't launch (at least) one activity.
The activity is declared in a library included as a module and is defined in this way in the library manifest.xml:
<activity android:name="com.my.company.core.views.download.VersionActivity"
android:label="#string/app_name"
android:exported="false"
android:screenOrientation="portrait" />
And I am trying to launch that activity in the following way:
Intent intent = new Intent(CurrentActivity.this, VersionActivity.class);
startActivity(intent);
If I have only one app, for example "My New App" installed in the terminal it works perfect. But if I have installed both apps "My New App" and "My Previous App" when I try to launch the activity as described Android says "No application can perform this action".
Can't I share code between apps? Is there a problem with the declaration of the activity in thee manifest? By the way, I've tried with exported="true" but it didn't work anyway.
Thanks in advance.
You can specify that your current app should handle the intent using setPackage.
Intent intent = new Intent();
intent.setAction("com.my.company.core.views.download.VersionActivity");
intent.setPackage(context.getPackageName());
startActivity(intent);
I need my android app to be in background mode after a phone restart/power on.
Currently I am using the following code, so that my app successfully gets launched after a phone restart/power on.
AndroidManifest.xml:
<receiver android:enabled="true" android:name="my_package.BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BootUpReceiver.java:
public class BootUpReceiver extends BroadcastReceiver
{
private static SharedPreferences aSharedSettings;
#Override
public void onReceive(Context context, Intent intent)
{
aSharedSettings = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
boolean isUserLoggedIn = aSharedSettings.getBoolean(Key.AUTHENTICATED, false);
if(isUserLoggedIn)
{
Intent aServiceIntent = new Intent(context, MyHomeView.class);
aServiceIntent.addCategory(Intent.CATEGORY_HOME);
aServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(aServiceIntent);
}
}
}
As I said above, my app successfully gets launched after a phone restart/power on.
However, after the phone restart/power on, my app was in foreground mode. But I need my app to be in background mode.
Can anyone please say, how to make an app to be in background mode after a phone restart or power on.
I even tried by changing the intent category to
<category android:name="android.intent.category.HOME" />
But no use in it. Can anyone please help me?
Thanks.
I need my app to be just running in background after the phone restart, so that users can select from the minimized app
I think your approach is wrong. All you are trying to do now is to add icon of your app to recent apps list. Your app won't run in background and I think you don't really want it. Am I right?
Recent apps list managed by android and IMHO forcing your app to be in recent apps list is not a very good idea. User will start you app when he wants from launcher or icon on his desktop.
If your broadcast receiver is working fine and app is starting successfully then you can use the below code in your MyHomeView activity's onCreate method to go to the home screen.
Trick is to click HOME button programmatically when app starts.
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
you can pass some variable from the BroadcastReceiver to differentiate a normal request and BroadcastReceiver's request to make the above code conditional.
But if you want to execute it always in background then it would be better to use Service.
It is recommended to change your code to the service to run it in background.
The suggestion which Leonidos replied is correct.
However, Just a workaround for this:
In my BootUpReceiver, I had a seperate boolean flag for this! (Its a bad way. but just a workaround)
SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
aPrefEditor.putBoolean(Key.IS_DEVICE_RESTARTED, true);
aPrefEditor.commit();
In Oncreate method of MyHomeView:
boolean isDeviceRestarted = aSharedSettings.getBoolean(Key.IS_DEVICE_RESTARTED, false);
if(isDeviceRestarted)
{
SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
aPrefEditor.putBoolean(MamaBearKey.IS_DEVICE_RESTARTED, false);
aPrefEditor.commit();
moveTaskToBack(true);
}
Thanks
In my application I have an option to start navigation to selected POI. Basically what I want is to launch a turn-by-turn navigator from my application. The thing is I don't know which (if any) navigator is installed.
So, the question is how to start an intent by showing a list of suitable activities for navigation to the user first, letting him choose which one he would like to use? Also would be nice to find a way to pass extra parameters to selected activity (this sounds like an issue to me, since different navigation apps use different names for their extras, I guess).
In case it's not clear: I'm looking for a way to DISPLAY A LIST OF SUITABLE APPLICATIONS FOR NAVIGATION WITH THE OPTION TO MAKE ONE DEFAULT.
EDIT: Find here the implementation http://datamoil.blogspot.com/2011/04/android-universal-intent-to-start.html
The bad news is, there isn't a standard Intent URI for navigation.
Yes, google.navigation URIs exist, and an app may choose to support it.
The best solution I can think of is to:
Explicitly check for known apps
Implicitly check for apps hooking google.navigation: and perhaps geo: (but then you also get map apps)
You can enumerate the possible implicit targets using PackageManage.queryIntentActivities
Try:
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=New+York+NY"));
startActivity(intent);
First I used in my onTap method inside the button's listener:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=New+York+NY"));
mContext.startActivity(i);
Then in manifest simply use:
<activity android:name=".LaunchGPS" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This will open any Navigator App your phone has such as VZ Navigagtor, Google or whatever the phone is loaded with. Worked for me the first time perfectly. Hope this solves your problem.
I found more advantages using this code (just for to show available navigator apps)
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:"));
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(Intent.createChooser(intent, "Continues with:", CHOOSE_NAVIGATOR_ID);
} else {
// Handle failure...
}
I created a test Activity that installs a shortcut of itself on the Android Home screen. When you click a button, the Activity is supposed to remove the same shortcut it just created. However, nothing I do seems to delete the shortcut.
Here is the Java code (ShortcutTest.java):
import java.net.URISyntaxException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ShortcutTest extends Activity {
String shortcutUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addShortcut(getBaseContext());
Button button = (Button)findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
removeShortcut(getBaseContext());
finish();
}
});
}
public void addShortcut(Context context) {
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("com.telespree.android.client", "com.telespree.android.client.ShortcutTest");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutTest");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutUri = intent.toUri(MODE_WORLD_WRITEABLE);
context.sendBroadcast(intent);
}
public void removeShortcut(Context context) {
Intent intent = null;
try {
intent = Intent.parseUri(shortcutUri, 0);
} catch (URISyntaxException e) {
}
intent.setAction("com.android.launcher.permission.UNINSTALL_SHORTCUT");
context.sendBroadcast(intent);
}
}
Here is the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.telespree.android.client"
android:versionCode="1"
android:versionName="1.0">
<permission
android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="normal"
/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".ShortcutTest"
android:label="#string/app_name" android:theme="#android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<!--
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
-->
<uses-sdk android:minSdkVersion="7" />
</manifest>
I'm almost positive there is some kind of permissions problem, though I've seen other posts on the Internet that indicates this should be possible.
Any advice is greatly appreciated.
Thanks.
DEPRECATED; KEPT SOLELY FOR HISTORICAL PURPOSES
This answer was posted in 2014, when the described method relied on functionality that existed in most Android devices. However, as mentioned by Adrian-Costin Čšundrea, this functionality was removed a couple of years ago from Launcher3, which is the AOSP launcher upon which the Google Now Launcher is based1. The commit message said:
Removing support due to its flacky design. Removing a shortcut
causes a full reload. Also we do not have any concept of owner, so
any app can remove any shortcut.
As of March 2017, this launcher, too, is being phased out in favor of "Google Search Launcher Services", which means that manufacturers could integrate a certain Google library into their own custom launchers, instead of relying on a standardized launcher provided by Google.
Considering that each manufacturer is free to implement their launcher whichever way they want, and assuming some of them are based off Launcher3, it's difficult to tell which devices the method below will work on, as Launcher3 was made to run even on some Android 4.1 devices, which are among the oldest devices still in use.
Greetings!
I have just dealt with the same exact problem, and would like to share my experience after successfully resolving it. tl;dr - skip to "In Conclusion" below.
Some background:
While working on the "next version" of an app, a need arose to change the default entry point (i.e. to rename the "Main Activity"). This is frowned upon because users who would be upgrading from an old version will still have the old shortcut, pointing to the wrong place. In order to avoid problems as much as possible, on the first launch, unbeknownst to them, the old shortcut was to be replaced with a new one.
Step 1: Setting up a new entry point
This is the easiest part. To declare an entry point the only essential thing to do is to put the following <action ...> tag in the appropriate activity declaration inside your Manifest:
<activity
android:name="YOUR_PACKAGE_NAME.YOUR_ACTIVITY_NAME"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
What makes an entry point default in some sense, is that the launcher shortcut points to it. This is why developers usually also include this in the <intent-filter>:
<category android:name="android.intent.category.LAUNCHER"/>
It should be noted that every activity that has this in its <intent-filter> will create an item in your app drawer - this is why for most cases 1 instance is all you need.
Step 2: Figuring out how the old shortcut is working
Having a rooted device, I could access the database table where the launcher/homescreen/desktop items are stored (see image of what the SQLite entries looks like) that's located in:
/data/data/com.android.launcher/databases/launcher.db -> SELECT * FROM favorites`
Here's a more readable version of the highlighted entry from the image:
#Intent;
action=android.intent.action.MAIN;
category=android.intent.category.LAUNCHER;
launchFlags=0x10200000;
package=gidutz.soft.bluecard;
component=gidutz.soft.bluecard/.LoadingScreen;
end
Note the 0x10200000 - this is explained in Step 4 - Attempt 1 below.
Step 3: Figuring out what the Shortcut Uninstaller is expecting
Lines 38-42 in UninstallShortcutReceiver.java tell us that:
Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
if (intent != null && name != null) { ... }
Meaning that the "uninstallation intent" has to have both Intent.EXTRA_SHORTCUT_INTENT and Intent.EXTRA_SHORTCUT_NAME or else it will not even consider executing.
Step 4: Finding the Right Syntax
This is a case of trial an error with a happy ending.
Attempt 1: Reconstructing the intent
Intent oldShortcutIntent = new Intent();
oldShortcutIntent.setAction(Intent.ACTION_MAIN);
oldShortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
oldShortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED +
Intent.FLAG_ACTIVITY_NEW_TASK);
oldShortcutIntent.setPackage("gidutz.soft.bluecard");
oldShortcutIntent.setComponent(new ComponentName("gidutz.soft.bluecard",
".LoadingScreen"));
// The above line is equivalent to:
Intent oldShortcutIntent = new Intent(getApplicationContext(),LoadingScreen.class);
Intent uninstaller = new Intent();
uninstaller.putExtra(Intent.EXTRA_SHORTCUT_INTENT, oldShortcutIntent);
uninstaller.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Blue Card");
uninstaller.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(uninstaller);
Result: Icon not removed.
The 0x10200000 is actually a sum of two arguments as explained here.
Attempt 2: Using as-is code from viralpatel
Intent shortcutIntent = new Intent(getApplicationContext(),LoadingScreen.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Blue Card");
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
Result: Icon not removed.
Attempt 3: "Brute Force"
Trying to copy-paste the intent exactly as it appears in the launcher.db:
Intent intent = new Intent();
String oldShortcutUri = "#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=gidutz.soft.bluecard;component=gidutz.soft.bluecard/.LoadingScreen;end";
try {
Intent altShortcutIntent = Intent.parseUri(oldShortcutUri,0);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, altShortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Blue Card");
} catch (URISyntaxException e) {
}
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);
Result: Icon removed!!
In Conclusion
Make sure that your "Icon Uninstaller" intent uses the exact same URI used to create the icon you're trying to delete, by either storing the URI used to create it, or by obtaining it from launcher.db.
Wait about 2-3 seconds for the "icon removed" toast to appear.
Sources
1) This guide at viralpatel.net
2) Google's implementation of UninstallShortcutReceiver.java
3) This thread at xdadevelopers
P.S.
In order to simulate and debug a Google Play update (which keeps the old shortcut) I did the following:
Installed the old version of the app from the store - an icon with the "old shortcut" was automatically placed on my screen.
Backed-up my launcher.db using Total Commander.
Installed the new version through my IDE (you can also use an .apk for that) - the "old shortcut" was now gone.
Opened Total Commander and minimized it (so that a shortcut is available in the "ALT-TAB" menu).
Went to the Device Settings >> Apps >> ALL, found my launcher (for me it was "Trebuchet" since I'm on CM11) and Force stopped it.
ALT-TAB into Total Commander and restored the DB.
Clicked the hardware "home" button to re-launch the launcher.
Viola! The old shortcut was now restored.
Note1: In retrospective, it might have been easier to create the old shortcut manually using the URI obtained from the database instead of going through all backing-up and force-stopping ordeal.
Note2: I haven't tried removing icons belonging to other apps using this method, but it might just be crazy enough to work.
While both solutions from Dev-iL and Funt work be advised they do so until Marshmallow. With Android 6.0 (which has Launcher v3) Google has removed the UninstallShortcutReceiver because of its security problems (probably because it became apparent here). So do not expect it to work with Android 6.0. Hopefully in some future release it will be readded in a form or another.
PS: Normally this should be a comment, but I am not allowed to comment because of the reputation...
You need to setAction for shortcutIntent like:
shortcutIntent.setAction(Intent.ACTION_MAIN);
Try to use
public void removeShortcut(Context context) {
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutTest");
try {
Intent shortcutIntent = Intent.parseUri(shortcutUri, 0);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
} catch (URISyntaxException e) {
}
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
context.sendBroadcast(intent);
}
Note: You do not have to save shortcutUri to remove the shortcut. Instead you can use
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("com.telespree.android.client",
"com.telespree.android.client.ShortcutTest");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
try {
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
Intent.parseUri(shortcutIntent.toUri(0), 0));
} catch (URISyntaxException e) {
e.printStackTrace();
}
...
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
context.sendBroadcast(intent);
If you want to use intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); instead of
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
Intent.parseUri(shortcutIntent.toUri(0), 0));
then you need to set action for shortcutIntent each time , i.e. while installing as well as while uninstalling e.g. Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
It took me about an hour of debugging and trying out every example on stackoverflow, but the solution was very easy. There's a typo in your code: You need to use com.android.launcher.action.UNINSTALL_SHORTCUT (as opposed to permission, as it is in the Manifest)
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");