How to remove application shortcut from home screen on uninstall automatically?

天大地大妈咪最大 提交于 2019-12-01 05:15:44
Christopher Orr

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.

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);

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.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!