android: install app without user intervention

2020-03-31 07:29发布

问题:

I want to install an application on Android without user intervention. I am using the permission INSTALL_PACKAGES and I am installing the app in the "/download/" folder. Once the download is finished, a dialog box appears asking me to install the app. How to hide this dialog and install the application without user intervention?

回答1:

Sorry, You can't. Android framework doesn't allowed you to do that. Android has a some restriction for Security Purpose, don't try to break it.



回答2:

INSTALL_PACKAGES permission is only allowed to system apps, installed in /system/app.

For other applications, it's not possible.



回答3:

You can use the hidden API android.content.pm.IPackageInstallObserver by reflection :

public class PackageManagement {
public static final int INSTALL_REPLACE_EXISTING = 0x00000002;
public static final int INSTALL_SUCCEEDED = 1;

private static Method installPackageMethod;
private static Method deletePackageMethod;


static {
    try {
        installPackageMethod = PackageManager.class.getMethod("installPackage", Uri.class, IPackageInstallObserver.class, Integer.TYPE, String.class);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}


public static void installPackage(PackageManager pm, Uri mPackageUri, IPackageInstallObserver observer, int installFlags, String installerPackageName) {
    try {
        installPackageMethod.invoke(pm, mPackageUri, observer, installFlags, installerPackageName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Import android.content.pm.IPackageInstallObserver into your Project. Your app must be system. You must activate the permission android.permission.INSTALL_PACKAGES in your manifest