Reputation: 21
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});
}
Upvotes: 0
Views: 7255
Reputation: 12656
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);
Upvotes: 2
Reputation: 7435
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..
Upvotes: 1