Your first bet is to look into Android's native PackageInstaller. I would recommend modifying that app the way you like, or just extract required functionality.
Specifically, if you look into PackageInstallerActivity and its method onClickListener
:
public void onClick(View v) {
if(v == mOk) {
// Start subactivity to actually install the application
Intent newIntent = new Intent();
...
newIntent.setClass(this, InstallAppProgress.class);
...
startActivity(newIntent);
finish();
} else if(v == mCancel) {
// Cancel and finish
finish();
}
}
Then you'll notice that actual installer is located in InstallAppProgress class. Inspecting that class you'll find that initView
is the core installer function, and the final thing it does is call to PackageManager
's installPackage
function:
public void initView() {
...
pm.installPackage(mPackageURI, observer, installFlags, installerPackageName);
}
Next step is to inspect PackageManager, which is abstract class. You'll find installPackage(...)
function there. The bad news is that it's marked with @hide. This means it's not directly available (you won't be able to compile with call to this method).
/**
* @hide
* ....
*/
public abstract void installPackage(Uri packageURI,
IPackageInstallObserver observer,
int flags,String installerPackageName);
But you will be able to access this methods via reflection.
If you are interested in how PackageManager
's installPackage
function is implemented, take a look at PackageManagerService.
Summary
You'll need to get package manager object via Context
's getPackageManager()
. Then you will call installPackage
function via reflection.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…