How to remove an application shortcut from the main screen when uninstalling automatically?

I am developing an application that should add its shortcut to the main screen after installation and delete it after uninstalling the application. The application will be pre-installed on the end-user device, but it must have an option for uninstalling). The task looks very simple, but I encountered many problems that implement it.

What I've done:

  • Add a shortcut to the main screen using com.android.launcher.action.INSTALL_SHORTCUT the first time you launch the application or on the device to connect new devices reboot.
  • MANUALLY remove the shortcut using com.android.launcher.action.UNINSTALL_SHORTCUT.

That I cannot (and almost refuse):

  • Automatically delete the shortcut when the application is uninstalled.

It is not possible to use Intent.ACTION_PACKAGE_REMOVED because the application being deleted does not receive this intention. I performed some tests and found out that the only type of shortcut that can be deleted using the application is the shortcut created in the menu "Add to Home Screen =>" Shortcuts "=" Applications ">" Application Activity ". Shortcuts that are created programmatically or declared in AndroidManifest, remain on the main screen after the application is uninstalled.

There are almost no documents and forum posts on this issue, and I'm a little confused why such a simple operation, which does not contradict the Android security policy, cannot be implemented directly.

Is there any way to ask the OS to remove the corresponding shortcut when uninstalling the application? Can I catch an event that the application is being deleted before it is deleted?

+6
android homescreen android-launcher
source share
3 answers

I do not believe that you can do this.

Firstly, because you cannot delete applications that are pre-installed on the device’s firmware and - they exist in the /system section, which is a read-only file system.

Secondly, as you noticed, your application does not receive notification of its removal .

If users don’t want to use your application, will they ignore the application icon, as I do for several pre-installed applications on my phone?


Edit:
If you intend to preinstall applications (but not firmware as commonsware.com notes), you can preinstall two APKs. One of which does not have a launcher and consists only of a broadcast receiver that processes the ACTION_PACKAGE_REMOVED event and calls UNINSTALL_SHORTCUT .

I do not believe that there are any explicit permissions checks that require the shortcut to be removed by the same application that added it, but you can get around this using sharedUserId for both sharedUserId .

+2
source share

It seems you are not using the install_shortcut intent correctly. Perhaps you are creating an intent without any parameters. You must create an intent using the Intent.ACTION_MAIN action.

 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); 
+2
source share

What you are describing is a limitation of the main screen. The next version of Launcher2 fixes this problem and automatically removes widgets and shortcuts associated with the application. Some shortcuts may be left if the connection is not found (if your application creates a shortcut for the music player, for example).

+1
source share

All Articles