15

I want to uninstall my application on button click. For this I am using following code.

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

It gives me result, but I want to delete directly without click on "Ok" button of dialog with message "This application will be Uninstalled".

I just want uninstalling application directly.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Android Leo
  • 666
  • 1
  • 8
  • 24
  • 2
    You need to have root permission to do that. – xDragonZ May 07 '12 at 14:06
  • 2
    This sort of stuff is reserved for system applications for good reasons - so you're stuck with rooting your device. – Jens May 07 '12 at 14:07
  • 1
    I think you should step back and think about why you would want to do this anyway. Why would you need to override the standard method for removing an application? – Kevin Coppock May 07 '12 at 14:38
  • @xDragonZ do you have any idea how to get root permission? it's about rooting of device can you guide me how to code for rooted device. – amity Jun 08 '12 at 07:10
  • @amity Just exec the command `su` and within that Process you have root priviliges: `Process p = Runtime.getRuntime().exec("su");` See this [blog](http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/) post for full example. – xDragonZ Jun 14 '12 at 12:33
  • @kcoppock I want to do that on an instance when device get stolen and I dont want my application to be on the device. At that time I want my application to uninstall. – Android Feb 26 '13 at 07:00

3 Answers3

11

Uninstalling without user confirmation is not allowed to 3rd party applications.

As xDragonZ points out, a root process can crudely do this by literally removing the directory and leaving the package manager to deal with the loss, but that's not a very widely deployable solution, since AFAIK no devices ship with that capability for apps to run their own root helper process - that's a risky aftermarket modification.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
5

Yes it is possible to uninstall a package in Android. Moreover you can also skip asking user to press OK button on uninstall screen. You can do it by using Accessibility service in Android.

public class MyAccessibilityService extends AccessibilityService {
    private static final String TAG = MyAccessibilityService.class
            .getSimpleName();

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.i(TAG, "ACC::onAccessibilityEvent: " + event.getEventType());

        //TYPE_WINDOW_STATE_CHANGED == 32
        if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event
                .getEventType()) {
            AccessibilityNodeInfo nodeInfo = event.getSource();
            Log.i(TAG, "ACC::onAccessibilityEvent: nodeInfo=" + nodeInfo);
            if (nodeInfo == null) {
                return;
            }

            List<AccessibilityNodeInfo> list = nodeInfo
                    .findAccessibilityNodeInfosByViewId("com.android.settings:id/left_button");
            for (AccessibilityNodeInfo node : list) {
                Log.i(TAG, "ACC::onAccessibilityEvent: left_button " + node);
                node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }

            list = nodeInfo
                    .findAccessibilityNodeInfosByViewId("android:id/button1");
            for (AccessibilityNodeInfo node : list) {
                Log.i(TAG, "ACC::onAccessibilityEvent: button1 " + node);
                node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        }

    }

    @Override
    public void onServiceConnected() {
        Log.i(TAG, "ACC::onServiceConnected: ");
    }

    @Override
    public void onInterrupt() {
        // TODO Auto-generated method stub

    }
}
Subham Gupta
  • 341
  • 4
  • 8
  • Note that this is only useful for apps that already use an Accessibility service. Otherwise it's of no use since prompting a user to enable an accessibility service inconveniences them more than just pressing "Ok" on an uninstall dialog. –  Jun 23 '21 at 13:59
1

You should first look into the Android native PackageInstaller. I would recommendating you to update all the code you use.


Next step is to inspect PackageInstaller which is an normal class. You will find that uninstall function there. The bad news is that this needs Manifest.permission.DELETE_PACKAGES permission and its only granted to system apps. This means that this is not available directly to other developers. But we can access it using device owner permission.


This requires:

  • Android 6.0 or newer
  • Device owner permission to uninstall the package

Generally the DELETE_PACKAGES permission says:

Allows an application to delete packages.

Not for use by third-party applications.

Once your app gets the device owner permission, you can uninstall an package like this:

String appPackage = "com.your.app.package";
Intent intent = new Intent(getApplicationContext(), 
getApplicationContext().getClass()); //getActivity() is undefined!
PendingIntent sender = PendingIntent.getActivity(getActivity(), 0, intent, 0);
PackageInstaller mPackageInstaller = 
getActivity().getPackageManager().getPackageInstaller();
mPackageInstaller.uninstall(appPackage, sender.getIntentSender());

The code used available here:

PackageInstaller "Silent install and uninstall of apps by Device Owner” - Android M Preview

Community
  • 1
  • 1