androidmdmdevice-admindevice-owner

Can a device admin in android install a package?


Is it possible to install an APK once the application has become device admin. I tried installing package using the code below but it didn't work.

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();
}

Solution

  • The Device Admin cannot silently install apps. Only the Device Owner can silently install apps.

    To set the app as the device owner, use one of the following options:

    1. Run the following command in adb shell:

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

    You must first implement the BroadcastReceiver which receives the event of being the Device Owner. You can look for the sample code here: https://github.com/h-mdm/hmdm-android (this is my open source MDM project).

    1. Use a QR code based enrollment: 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