You should first look into the Android's native Package Installer. I think you just need to extract the required functionality.
Specifially, if you look at this method and its OnClickListener:
public void onClick(View v) {
if(v == mOk) {
...
startActivity(newIntent);
finish();
} else if(v == mCancel) {
// Cancel and finish
setResult(RESULT_CANCELED);
finish();
}
}
Then you may notice the InstallAppProgress class on where the actual installer is located and the final thing to do is to call the PackageManager
's installPackage(...) function.
public void initView() {
...
pm.installPackage(mPackageURI, observer, installFlags, installerPackageName);
}
Next step is to inspect PackageManager
which is an abstract class. You will find that installPackage(...) function there. The bad news is that this is marked with @hide
. This means it's not directly available to external developers.
/**
* @hide
**/
public abstract void installPackage(Uri packageUri, IPackageInstallObserver installObserver, int flags, String installerPackageName);
But you will be able to access the method with using reflection.
Summary
You will reflect it using yourContext.getPackageManager()
. Then you may call the installPackage(...) function.
The following code calls the installation:
try {
getPackageManager().installPackage(packageUri, myObserver, PackageManager.INSTALL_REPLACE_EXISTING, "com.your.package.name");
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
For the whole thing to work you need to declare this to your manifest or the code will fail silently.
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
to get this permission you must install the APK as system which requires ROOT.
however after you have installed the APK as system it seem to work WITHOUT ROOT.
To do this, i created a signed APK and pushed it on:
adb push C:\Users\Example01\Desktop\release\app-release.apk /system/priv-app/MyApp.apk
with this i copied it to /system/priv-app
which requires write access this is why the ROOT required.
I tested it using the debug version and when i tried it, i got the SecurityException
.
Conclusion
You should use a rooted device to install that as an sytem priveligied app.
Hope this helps :)