Reputation: 4223
How to remove shortcuts in the home screen(programatically). I can remove the shortcuts created by me using intent but not other existing ones..
Bitmap theBitmap = ((BitmapDrawable)icon).getBitmap();
Intent shortcutIntent = new Intent();
shortcutIntent.setAction(Intent.ACTION_VIEW);
shortcutIntent.setClassName(url, group);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
removeIntent.putExtra("duplicate", false);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, theBitmap);
removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(removeIntent);
when deleting using intent as above, it is possible to delete shortcut when knowing the icon packagename + app class name. we can get icon package name of all application using PackageInfo as follows but not the launch class name
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.classname = p.applicationInfo.className;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
Now problem is, Is there any method to get class name of a application, then we can delete the shortcut created by this application in the home Screen.. any help??
Upvotes: 4
Views: 4967
Reputation: 3512
It IS possible.
[UPDATE]: Since Android M, they've completely removed support for the UNINSTALL_SHORTCUT intent in AOSP Launcher3 and Google Now Launcher. Currently there is no alternative or replacement for this API. Source: https://code.google.com/p/android/issues/detail?id=179697
[UPDATE-2]: Maybe this is helpful for some of you. I was able to figure out, how - at least- to uninstall my apps own shotscuts from the home screen. 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.
Small reference:
ComponentName yourAlias = new ComponentName(getPackageName(), "org.example.yourAliasName");
if (show){
flag = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
}else{
flag = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}
context.getPackageManager().setComponentEnabledSetting(yourAlias, flag, PackageManager.DONT_KILL_APP);
Below Android M:
First things first, here the code which I was repeatedly able to delete the Facebook shortcut, without any root (which is kind of the same like op's code, so he was not wrong):
Intent appIntent = new Intent(Intent.ACTION_MAIN);
appIntent.setComponent(new ComponentName("com.facebook.katana", "com.facebook.katana.LoginActivity"));
appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Facebook");
addIntent.putExtra("duplicate", false);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, appIntent);
sendBroadcast(addIntent);
The important thing is, that your intent must be exaclty the same, as the Intent which
was used to create the shortcut. You can, if you have root, take a look into the
launcher.db and copy the URI.
Located in data/data/com.android.Launcher.launcher.db
However, you don't have access to the android launcher.db
anytime, so it can be a bit try and error.
Sources which I highly recommend to read:
UninstallShortcutReceiver.java
very good answer from Dev-il for this topic
Additonally, code to fetch all applications and their data, which in my case was enough to remove most of the shortcuts.
PackageManager pManager = context.getPackageManager();
List<ResolveInfo> packageInfos = loadApps(context);
From here you can iterate over the list and fetch necessary information's like:
ActivityInfo activityInfo = packInfo.activityInfo;
//name of the App
//Icon of the activity as drawable
// packageName the activity belongs to
//Launcher class name
Upvotes: 6
Reputation: 1444
Use the following method:
private void removeShortcutIcon(String app_name, Intent app_intent){
app_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
app_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, app_intent);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, app_name);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(removeIntent);
}
And try to get all installed apps in the following way:
PackageManager pm = getPackageManager();
List<ResolveInfo> pacs_list = pm.queryIntentActivities(main_intent, 0);
for(int i=0; i < pacs_list.size(); i++)
{
pacs_list.get(i).loadIcon(pm);
pacs_list.get(i).activityInfo.packageName;
pacs_list.get(i).loadLabel(pm).toString();
}
Note: you can get various attributes from ResolveInfo. Check this link: ResolveInfo
Upvotes: 0
Reputation: 928
you cannot remove programatically other shortcuts from homescreen
Upvotes: 1