如何使用 PackageInstaller 为配置文件所有者安装应用程序?
How to install app using PackageInstaller for Profile Owner?
我正在尝试使用 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;
}
您的应用必须设置为设备所有者或附属个人资料所有者。
我正在尝试使用 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;
}
您的应用必须设置为设备所有者或附属个人资料所有者。