I've Device admin app which I want to update by manual installing.The problem is when app is in admin mode I'm not getting the install pop up generated by android to install apk but when unpinned or remove app as device admin I am getting that install dialog. I've approached two methods
First Method :Install apk using intent
Intent intentInstall = new Intent(Intent.ACTION_VIEW);
intentInstall.setDataAndType(Uri.fromFile(new File(Constants.UPDATE_APK_PATH)), "application/vnd.android.package-archive");
intentInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
AppApplication.getContext().startActivity(intentInstall);
This method works only when app in not set as device admin
Second Method :Silent Install using PackageManger
PackageManager packageManger = mContext.getPackageManager();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
PackageInstaller packageInstaller = packageManger.getPackageInstaller();
String packageName = mContext.getPackageName();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(packageName);
try {
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
OutputStream out = session.openWrite(packageName + ".apk", 0, -1);
readTo(Constants.UPDATE_APK_PATH, out); //read the apk content and write it to out
session.fsync(out);
out.close();
System.out.println("installing...");
session.commit(PendingIntent.getBroadcast(mContext, sessionId,
new Intent("android.intent.action.MAIN"), 0).getIntentSender());
System.out.println("install request sent");
} catch (IOException e) {
e.printStackTrace();
}
}
System.err.println("old sdk");*/
private static void readTo(String updateApkPath, OutputStream out) {
try {
File file = new File(updateApkPath);
out = new FileOutputStream(file);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I did little research for package manger and came across this solution but this does not work in my case
In short I want to update my device admin app by manual install.
Any help will be appreciated