0

I want to delete application silently from device. i am trying this code but gives exception "Neither user 10051 nor current process has android.permission.DELETE_PACKAGES."

 class PackageInstallObserver extends IPackageInstallObserver.Stub {
    public void packageInstalled(String packageName, int returnCode) throws RemoteException {
        if (onInstalledPackaged != null) {
            onInstalledPackaged.packageInstalled(packageName, returnCode);
        }
    }
}

class PackageDeleteObserver extends IPackageDeleteObserver.Stub { 

    public void packageDeleted(String packageName, int returnCode) throws RemoteException {
        /*if (onInstalledPackaged != null) {
            onInstalledPackaged.packageInstalled(packageName, returnCode);
        }*/
    }
}

public ApplicationManager(Context context) throws SecurityException, NoSuchMethodException {

    observer = new PackageInstallObserver();
    observerdelete = new PackageDeleteObserver(); 
    pm = context.getPackageManager();



    Class<?>[] types = new Class[] {Uri.class, IPackageInstallObserver.class, int.class, String.class};
    Class<?>[] uninstalltypes = new Class[] {String.class, IPackageDeleteObserver.class, int.class};

    method = pm.getClass().getMethod("installPackage", types);
      uninstallmethod = pm.getClass().getMethod("deletePackage", uninstalltypes);
}

public void setOnInstalledPackaged(OnInstalledPackaged onInstalledPackaged) {
    this.onInstalledPackaged = onInstalledPackaged; 
}

public void uninstallPackage(String packagename) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    uninstallmethod.invoke(pm, new Object[] {packagename, observerdelete, 0});


}
pagid
  • 13,559
  • 11
  • 78
  • 104
Govind
  • 21
  • 1
  • 1

2 Answers2

2

It is impossible to silently delete an Android app programmatically.

The best you can do is ask the user to delete the app in a Dialog. When he presses "OK" then redirect to the Uninstaller application pointed to the application. It is still the user's choice if he wants to complete the delete operation.

There is a way to insist that the user finishes the delete operation, but it is rather bullish. After he closes the Uninstaller application he will return to your Activity in onActivityResult(). You can check to see if the app is deleted. If it is not yet deleted you can return to the same Dialog and ask him again to delete the application. You can continue in this endless loop if you choose until the user finally deletes the app or stops using your main application (i.e. You are telling the user he can't use your app until he deletes the other app).

How to check if an app is installed:

final String packageName = "com.company.other-app-name";
android.content.pm.PackageManager pm = getPackageManager();
PackageInfo info = pm.getPackageInfo(packageName, 0);

If info==null or a NameNotFoundException is thrown, then the app is not installed.

How to launch the uninstaller pointed to an app:

Uri uri = Uri.parse("package:com.company.other-app-name");
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivityForResult(intent, REQUEST_CODE_UNINSTALL_FREE_APP);
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • "If info==null ..." is not true, null is never returned from getPackageInfo. – Pointer Null Dec 11 '13 at 09:03
  • It is not impossible, if the app has system permissions (e.g. signed with firmware key), it can acquire the `DELETE_PACKAGES` permission. Refer to https://stackoverflow.com/q/5803999/10086742 – mahdi Aug 04 '21 at 08:15
1

SDK applications cannot have DELETE_PACKAGES permissions, unless they are part of the firmware. Which means that an app that is not compiled with firmware does not have/acquire right to delete apps silently..

check out this post for more info..

Community
  • 1
  • 1
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
  • how do apps like "Batch Uninstaller" work without the need to be system apps? do they actually do the same operations needed for uninsalling, instead of using "adb uninstall"? – android developer Mar 21 '13 at 20:30
  • this is from there play store page "Only ROOT phones or phones with ROOT right can perform silent uninstall which will not pop up any dialog box and no need to confirm repeatedly. Just one touch to batch uninstall. The phone without ROOT will need to select repeatedly in system interface." https://play.google.com/store/apps/details?id=cn.menue.BatchUninstall&hl=en – Praful Bhatnagar Mar 22 '13 at 05:05
  • ok, so it is possible. for some reason though, some permissions can't be achieved even though you have root, like toggling the GPS. – android developer Mar 22 '13 at 08:48