6

I'm developing an application that should add its shortcut to home screen after installation and remove it after the application is being uninstalled. The application will be preinstalled on the end user device, but still should have an option for uninstall). The task looks very simple but I've faced lots of troubles implementing it.

What I have done:

  • Add shortcut to the home screen using com.android.launcher.action.INSTALL_SHORTCUT on app first launch or on newt device reboot.
  • MANUALLY remove shortcut using com.android.launcher.action.UNINSTALL_SHORTCUT.

What I can't (and almost giving up):

  • Automatically remove the shortcut when the application is being uninstalled.

There's not way to use Intent.ACTION_PACKAGE_REMOVED because the application being uninstalled does not receive this intent. I performed some tests and found out that the only shortcut type that is being removed with the application is the shortcut that is created from menu 'Add to home screen => Shortcuts => Applications => Application activity'. The shortcuts that are being created programatically or that are declared in AndroidManifest remain on home screen after the app is uninstalled.

There's almost none docs and posts on forums about this topic and I'm confused a little bit why such a simple operation that doesn't contradict with the Android security policy could not be implemented in a straight way.

Is there any way to ask OS to remove the corresponding shortcut on application uninstall? Can I catch the event that the application is being uninstalled before it is removed?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vladimir Dmi
  • 161
  • 2
  • 6

3 Answers3

2

Seems that you don't use install_shortcut intent in right way. Probably you create an intent without any parameters. You should to create intent with an action Intent.ACTION_MAIN param.

Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());

Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
            this,  R.drawable.launcher_icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
sendBroadcast(intent);
Lazy Beard
  • 305
  • 3
  • 10
2

I don't believe you can do this.

Firstly because you cannot uninstall applications that are pre-installed on the device firmware — they exist on the /system partition which is a read-only filesystem.

Secondly, as you note, your application receives no notification that it is being uninstalled.

If users may not want to use your application, won't they just ignore the application icon, much like I do for a couple of pre-installed apps on my phone?


Edit:
If you are going to pre-install apps (but not on the firmware as commonsware.com notes), you could pre-install two APKs. One of which has no launcher and consists only of a broadcast receiver which handles the ACTION_PACKAGE_REMOVED event and calls UNINSTALL_SHORTCUT.

I don't believe there is any explicit permission checks that require a shortcut to be removed by the same app that added it, but you could get around that anyway by using a sharedUserId for both APKs.

Community
  • 1
  • 1
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • Note that "pre-installed" does not necessarily imply "firmware". It is possible to have an application installed as if it were a normal application, but just so happens to ship on the device in advance. The ARCHOS 5 Android tablet did that with a dozen or so apps, for example. – CommonsWare Jan 25 '10 at 12:13
  • Indeed. I'm working with devices that *will* have apps installed on the firmware, so I made sure to mention the firmware part. But I think the problem will still stand anyway due to the lack of PACKAGE_REMOVED broadcast. – Christopher Orr Jan 25 '10 at 12:33
  • Though your comment prompted me to add a potential solution for non-firmware pre-installs. :) – Christopher Orr Jan 25 '10 at 12:40
  • Christopher, thanks for the idea with two applications, I'll check if it's suitable for my application. – Vladimir Dmi Jan 26 '10 at 21:38
1

What you are describing is a limitation of the Home screen. The next version of Launcher2 addresses this issue and automatically removes widgets and shortcuts associated with an app. Some shortcuts might be left though if no association can be found (if your app creates a shortcut to the music player for instance.)

Romain Guy
  • 97,993
  • 18
  • 219
  • 200