Angela Sim
Angela Sim

Reputation: 137

How to uninstall a running app through code?

I have a requirement in my app that after a specified number of unsuccessful login attempts:

  1. A folder on the sdcard essential for the app be deleted and
  2. The app be uninstalled from the device.

This is basically a self destruct sort of action.

Can you provide inputs on whether the aspect of uninstalling the app, while the app is running is feasible? I assume deletion of the folder must be easy. Do you foresee any issues with doing that too.

Thanks A

Upvotes: 1

Views: 1373

Answers (2)

Menno
Menno

Reputation: 1143

Larry Schiefer is right, there is (fortunately) no public API to do this without user interaction. But if you have extended rights, for example if the device is rooted and your app has root access, you could use the command line:

Runtime.getRuntime().exec(new String[] {"su", "pm uninstall com.example.yourapplication"})

In the case you have special privileges because you are a system app, you can use hidden API's, which are very dangerous because they are undocumented and can change from one Android update to the next. However, if you are in control which version of the OS installed, you might get away with it.

In this case, you could call PackageManager.deletePackage through reflection.

pm = context.getPackageManager();
Class<?>[] types = new Class[] {String.class, IPackageDeleteObserver.class, int.class};
method = pm.getClass().getMethod("deletePackage", types);
method.invoke(pm, new Object[] {"com.example.com", null, 0})

This function should also delete all your package data, so it's a complete wipe. You need to ask for the DELETE_PACKAGES permission in your AndroidManifest.xml (which is only granted if you are a system app/system user):

<uses-permission android:name="android.permission.DELETE_PACKAGES"/>

Upvotes: 0

Larry Schiefer
Larry Schiefer

Reputation: 15775

There's no public API to allow you to seamlessly remove a package (including your own.) You can request a package be removed by firing an Intent with the action set to ACTION_UNINSTALL_PACKAGE, but the user will be notified of the action as it will take them to the default installer (ie Google Play Store) to perform the action.

Upvotes: 1

Related Questions