android 中的设备管理员可以安装软件包吗?

Can a device admin in android install a package?

一旦应用程序成为设备管理员,是否可以安装 APK。 我尝试使用下面的代码安装包,但没有成功。

private static final String ACTION_INSTALL_COMPLETE = "com.test.sampleapp_dev.INSTALL_COMPLETE";

public static boolean installPackage(Context context, InputStream in, String packageName)
        throws IOException {
    PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);
    // set params
    int sessionId = packageInstaller.createSession(params);
    PackageInstaller.Session session = packageInstaller.openSession(sessionId);
    OutputStream out = session.openWrite("COSU", 0, -1);
    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(createIntentSender(context, sessionId));
    return true;
}

private static IntentSender createIntentSender(Context context, int sessionId) {
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            context,
            sessionId,
            new Intent(ACTION_INSTALL_COMPLETE),
            0);
    return pendingIntent.getIntentSender();
}

设备管理员无法静默安装应用程序。只有设备所有者才能静默安装应用程序。

要将应用设置为设备所有者,请使用以下选项之一:

  1. 运行 在 adb shell 中执行以下命令:

dpm set-device-owner com.yourpackage.id/.AdminReceiver

您必须首先实现 BroadcastReceiver 来接收成为设备所有者的事件。您可以在此处查找示例代码:https://github.com/h-mdm/hmdm-android(这是我的开源 MDM 项目)。

  1. 使用二维码注册:https://developers.google.com/android/management/provision-device Here's a sample video of the QR code based enrollment: https://www.youtube.com/watch?time_continue=47&v=l2ZxS_GonWM