10

Background

The normal way to call for the uninstallation an app is simply by using the "ACTION_DELETE" intent :

startActivity(new Intent(Intent.ACTION_DELETE, Uri.parse("package:" +packageName)));

The problem

starting with some Android version (don't remember which) , apps can be installed for multiple users on the same device.

This means there is a new way to uninstall an app, one which will uninstall it for all users (image taken from Lollipop - Android 5.0 ) :

enter image description here

The question

I've searched in the documentation, but couldn't find the answer those questions:

  1. Is there any way to perform this operation via an intent? Maybe something to add to the intent I've written above ?

  2. Does ADB have a new command to remove an app for all users?

  3. Is there a way to check if an app is installed for multiple users?

adneal
  • 30,484
  • 10
  • 122
  • 151
android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

11

Is there any way to perform this operation via an intent? Maybe something to add to the intent I've written above ?

Yes, but be careful. You can pass in Intent.EXTRA_UNINSTALL_ALL_USERS.

However, it's hidden because it:

should not be part of normal application flow

You could just pass in the constant anyway, if you feel it's necessary and disagree with Google on that one. Just for example, here are the differences between passing in false and true with that constant

    final Uri packageURI = Uri.parse("package:" + "some.package.name");
    final Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
    uninstallIntent.putExtra("android.intent.extra.UNINSTALL_ALL_USERS", false or true);
    startActivity(uninstallIntent);

Results

example

Does ADB have a new command to remove an app for all users?

No, the command remains the same.

`adb uninstall 'some.package.name'` 

This will remove that app for all users. I'm unaware of a way to specify a particular user.

Is there a way to check if an app is installed for multiple users?

No, not that I'm aware of. In fact, when the Settings apps decides to place the "Uninstall for all users" option in the options menu, it's basically doing so based on whether or not there are multiple users period, not if both the current user and another user have an app installed.

Not to mention, most of the methods in UserManager that you'd need to even tell if there are multiple users on the device, like UserManager.getUserCount, require the MANAGE_USERS permission which is a system API and hidden. So, I'm not even sure why that's a public method.

Also, you can easily test all of your questions, much like I did, by creating a dummy user on your device. You don't even need to log into a Google account.

adneal
  • 30,484
  • 10
  • 122
  • 151
  • Thank you. I need to know if an app is installed for multiple users for my app (here: https://play.google.com/store/apps/details?id=com.lb.app_manager ) , so that I will be able to let the user to choose if uninstallation would be for all users or just the current one. Multi-user support has started in fact from Android 4.2, and I can't find the needed information. – android developer Feb 27 '15 at 13:49
  • I think I can use PackageManager->getPackagesForUid (here: http://developer.android.com/reference/android/content/pm/PackageManager.html#getPackagesForUid(int) ) to get the packages of a specific user, but in order to get the list of users-ids, I need to use UserManager->getUserProfiles and then getSerialNumberForUser " , but "getUserProfiles requires API 21 and the permission of "MANAGE_USERS" can't even be given (as you wrote)... Anyway, I will tick your answer and create a new question – android developer Feb 27 '15 at 23:19
  • You wrote "it's basically doing so based on whether or not there are multiple users period". How do I know that there are multiple users accounts? Is it possible that having at least one folder on "/mnt/user" mean there are multiple users? Also, do all adb commands affect all users (like clearing data of apps), and not just the current one ? – android developer Oct 26 '15 at 11:35
  • 2
    For adb you can run: `adb uninstall --user -1 'some.package.name'` - it is mean uninstall for all users, because -1 = `UserHandle.USER_ALL` and `if (userId == UserHandle.USER_ALL) { userId = UserHandle.USER_OWNER; flags |= PackageManager.DELETE_ALL_USERS; }` – Enyby Mar 03 '17 at 18:53
  • Is there any function like onDestroy? so that I can run the code while users uninstall my app. – Simon Jul 23 '18 at 06:19