Search code examples
androiddialogsystemlauncher

Getting user's answer (System dialog - Android)


I am a newbie on Android and I am creating a launcher. I want apps to be removed (uninstalled) so I have a list and I invoke system to uninstall it.

How can I know if the user pressed "Cancel" or "Ok" in the system dialog? (I know the system will unisntall the app if I press "Ok" or wont if I press "Cancel", I just need to know how to get the answer to remove or not the app of my list [ArrayList]).

If you cant know it, how can I do to remove an app from a list without knowing if the user is going to uninstall it or not?

public void uninstall (int position){
    Uri package1 = Uri.parse("package:"+apps_block.get(position).name.toString());
    Intent uninstall = new Intent(Intent.ACTION_DELETE, package1);
    startActivity(uninstall);
    AppDetail toRemove = adapter_block.getItem(position);
    adapter_block.remove(toRemove);
}

With this code, the app is always removed from my list even I press "Cancel".


Solution

  • You are removing the item from the list immediately after startActivity(). The user has not even seen the dialog yet by that point.

    You could listen for the ACTION_PACKAGE_REMOVED system broadcast, confirm that it was for your requested package, and remove the package from the list at that point. By doing this from your activity via registerReceiver(), you can find out fairly quickly and have easy access to your UI code to update the list.