install / uninstall APKs programmatically (PackageManager vs Intents)
This is not currently available to third party applications. Note that even using reflection or other tricks to access installPackage() will not help, because only system applications can use it. (This is because it is the low-level install mechanism, after the permissions have been approved by the user, so it is not safe for regular applications to have access to.)
Also the installPackage() function arguments have often changed between platform releases, so anything you do trying access it will fail on various other versions of the platform.
EDIT:
Also it is worth pointing out that this installerPackage was only added fairly recently to the platform (2.2?) and was originally not actually used for tracking who installed the app -- it is used by the platform to determine who to launch when reporting bugs with the app, for implementing Android Feedback. (This was also one of the times the API method arguments changed.) For at least a long while after it was introduced, Market still didn't use it to track the apps it has installed (and it may very well still not use it), but instead just used this to set the Android Feedback app (which was separate from Market) as the "owner" to take care of feedback.
Uninstall APKs programmatically for all user
final Uri packageURI = Uri.parse("package:" + "package:com.example.mypackage");
final Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
uninstallIntent.putExtra("android.intent.extra.UNINSTALL_ALL_USERS", false or true);
startActivity(uninstallIntent);
You can check out this answer from where I've taken reference - Is there an intent for uninstallation of an app for ALL users?
What is mean by multiple user?
If you have a used laptop or machine, then you may already be familiar with the concept here: Where you can create multiple accounts with different roles. It’s like having multiple machines wrapped into one.
Android has a very similar feature built in called User Profiles. It will create a separate profiles where one can make changes and keep multiple settings. But yeah when users installs app it will automatically will be installed in all profiles so end user need manually uninstall it in the profile where it is not required.
I tried using this in my device but performance is bit slow after I added multiple profile.
How to uninstall apps in android programmatically with PackageInstaller
You can refer to this link
https://www.programcreek.com/java-api-examples/index.php?api=android.content.pm.PackageInstaller
and implement like this -
@Override
public void uninstall(String packageName, String callerPackageName, int flags, IntentSender statusReceiver, int userId) throws RemoteException {
boolean success = VAppManagerService.get().uninstallPackage(packageName);
if (statusReceiver != null) {
final Intent fillIn = new Intent();
fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, packageName);
fillIn.putExtra(PackageInstaller.EXTRA_STATUS, success ? PackageInstaller.STATUS_SUCCESS : PackageInstaller.STATUS_FAILURE);
fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE, PackageHelper.deleteStatusToString(success));
fillIn.putExtra("android.content.pm.extra.LEGACY_STATUS", success ? 1 : -1);
try {
statusReceiver.sendIntent(mContext, 0, fillIn, null, null);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
uninstall app silently with system privileges
Here is how I did it:
ApplicationManager.java (changed part):
private PackageInstallObserver observer;
private PackageDeleteObserver observerdelete;
private PackageManager pm;
private Method method;
private Method uninstallmethod;
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 uninstallPackage(String packagename) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
uninstallmethod.invoke(pm, new Object[] {packagename, observerdelete, 0});
}
PackageDeleteObserver.java (in android.content.pm):
package android.content.pm;
public interface IPackageDeleteObserver extends android.os.IInterface {
public abstract static class Stub extends android.os.Binder implements android.content.pm.IPackageDeleteObserver {
public Stub() {
throw new RuntimeException("Stub!");
}
public static android.content.pm.IPackageDeleteObserver asInterface(android.os.IBinder obj) {
throw new RuntimeException("Stub!");
}
public android.os.IBinder asBinder() {
throw new RuntimeException("Stub!");
}
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags)
throws android.os.RemoteException {
throw new RuntimeException("Stub!");
}
}
public abstract void packageDeleted(java.lang.String packageName, int returnCode)
throws android.os.RemoteException;
}
Also dont forget to add permission to manifest:
<uses-permission android:name="android.permission.DELETE_PACKAGES"/>
Working sample project (apk need to be placed in "/system/app" path on device):
http://www.mediafire.com/file/no4buw54ed6vuzo/DeleteInBackgroundSample.zip
How to uninstall an Android app programmatically without user interaction?
try this method on rooted device
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("pm uninstall package_name\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
throw new Exception(e);
}catch(InterruptedException e){
throw new Exception(e);
}
Related Topics
Make an Http Request With Android
How to Dynamically Add Suggestions to Autocompletetextview With Preserving Character Status
What to Do on Transactiontoolargeexception
Android Listview With Different Layouts For Each Row
How to Check If a Service Is Running on Android
How to Enable or Disable the Gps Programmatically on Android
How to Connect to a Specific Wi-Fi Network in Android Programmatically
How to Change the Status Bar Color in Android
Find Location of a Removable Sd Card
How to Copy Files from 'Assets' Folder to Sdcard
Android: Install .Apk Programmatically
How to Add Parameters to Httpurlconnection Using Post Using Namevaluepair
Firebase Android: Make Username Unique
Firebaseinstanceidservice Is Deprecated
Re-Launch of Activity on Home Button, But...Only the First Time
Download Binary File from Okhttp