我正在尝试使用 Package Installer 安装 apk 文件。我的应用程序是当前的个人资料所有者。该方法成功完成,但在我的 BroadcastReceiver 中,我得到了 PackageInstaller.STATUS_PENDING_USER_ACTION。根据 android 文档https://developer.android.com/reference/android/content/pm/PackageInstaller应用程序应该能够静默安装 apk,如果它是配置文件所有者。
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;
}