我正在尝试使用软件包安装程序安装APK文件。我的应用程序是当前的个人资料所有者。该方法已成功完成,但是在我的Broadcastreceiver中,我将获得PackageInstaller.status_pending_user_action。根据Android Docs https://developer.android.com/reference/android/android/content/pm/packageinstaller应用程序,如果它是配置文件所有者,则应该能够默默安装。
public static boolean installPackage(Context context, InputStream
in,
String packageName)
throws IOException {
final PackageInstaller packageInstaller =
context.getPackageManager().getPackageInstaller();
final PackageInstaller.SessionParams params = new
PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(packageName);
// set params
final int sessionId = packageInstaller.createSession(params);
final PackageInstaller.Session session =
packageInstaller.openSession(sessionId);
final OutputStream out = session.openWrite("TestDPC", 0, -1);
final byte[] buffer = new byte[65536];
int c;
while ((c = in.read(buffer)) != -1) {
out.write(buffer, 0, c);
}
session.fsync(out);
in.close();
out.close();
session.commit(createInstallIntentSender(context, sessionId));
return true;
}
必须将您的应用设置为设备所有者或附属配置文件所有者。