Skip to content Skip to sidebar Skip to footer

Running Dpm With Runtime.exec(...)

This answer suggests that an Android app can run dpm like this: Runtime.getRuntime().exec('dpm set-device-owner com.test.my_device_owner_app'); This fails silently on my Nexus 4 r

Solution 1:

dpm incorrectly exits with a status code of 0 when you get the command syntax wrong. The correct syntax is dpm set-device-owner package/.ComponentName. When you get the syntax right, exec(...) throws a SecurityException:

java.lang.SecurityException: Neither user 10086 nor current process has android.permission.MANAGE_DEVICE_ADMINS.
  at android.os.Parcel.readException(Parcel.java:1546)
  at android.os.Parcel.readException(Parcel.java:1499)
  at android.app.admin.IDevicePolicyManager$Stub$Proxy.setActiveAdmin(IDevicePolicyManager.java:2993)
  at com.android.commands.dpm.Dpm.runSetDeviceOwner(Dpm.java:110)
  at com.android.commands.dpm.Dpm.onRun(Dpm.java:82)
  at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
  at com.android.commands.dpm.Dpm.main(Dpm.java:38)
  at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
  at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:249)

Adding this permission to the manifest does not help, so maybe it's a system-only permission.

It's already a pain in the butt to deploy a kiosk mode app on a device without NFC, since you have to enable developer mode and install the app via adb. I guess the provisioner will just have to run dpm manually.

Solution 2:

As some extra info, I was able to capture and log the output (stdout and stderr) to logcat

DevicePolicyManagerdpm= (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
Runtimert= Runtime.getRuntime();
Processproc=null;
try {
    proc = rt.exec("dpm set-device-owner com.myapp/.DeviceOwnerReceiver");
    BufferedReaderstdInput=newBufferedReader(newInputStreamReader(proc.getInputStream()));

    BufferedReaderstdError=newBufferedReader(newInputStreamReader(proc.getErrorStream()));

    // Read the output from the command
    System.out.println("Here is the standard output of the command:\n");
    Strings=null;
    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);
    }

    // Read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):\n");
    while ((s = stdError.readLine()) != null) {
        System.out.println(s);
    }
} catch (IOException e) {
    e.printStackTrace();
}


Post a Comment for "Running Dpm With Runtime.exec(...)"