1

I have been working on making the package as device owner but did not found any success. I have rooted my device for the same. I am using this command.

                val exe = ShellExecuter()
                var command = "dpm set-device-owner $packageName/ .MyDeviceAdminReceiver"
                val outp = exe.Executer(command)

ShellExecuter snippet

public String Executer(String command) {
                    StringBuffer output = new StringBuffer();
                    Process p;
                    try {
                        p = Runtime.getRuntime().exec(command);
                        p.waitFor();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                        String line = "";
                        while ((line = reader.readLine())!= null) {
                            output.append(line + "\n");
                        }
                    } catch (Exception e) {`enter code here`
                        e.printStackTrace();
                    }
                    String response = output.toString();
                    return response;
                } 

MyDeviceAdminReceiver snippet

class MyDeviceAdminReceiver : DeviceAdminReceiver() {
    companion object {
        fun getComponentName(context: Context): ComponentName {
            return ComponentName(context.applicationContext, MyDeviceAdminReceiver::class.java)
        }

        private val TAG = MyDeviceAdminReceiver::class.java.simpleName
    }

    override fun onLockTaskModeEntering(context: Context?, intent: Intent?, pkg: String?) {
        super.onLockTaskModeEntering(context, intent, pkg)
        Log.d(TAG, "onLockTaskModeEntering")
    }

    override fun onLockTaskModeExiting(context: Context?, intent: Intent?) {
        super.onLockTaskModeExiting(context, intent)
        Log.d(TAG, "onLockTaskModeExiting")
    }
}

device_admin_reciever snippet

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<device-admin>
    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
        <expire-password />
        <encrypted-storage />
        <disable-camera />
    </uses-policies>
</device-admin>

I want to make my rooted device owner of my app package programmatically using commands or any other way if anybody can suggest.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ravneet Singh
  • 213
  • 1
  • 13

2 Answers2

0

If you're root on your device, you can follow this method to become device owner.

First, create a file device_owner.xml with following content:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<device-owner package="your.owner.app.package.id" name="Your app name" />

Now do the following steps

adb push device_owner.xml /sdcard/

adb shell

su

cp /sdcard/device_owner.xml /data/system/

cd /data/system/

chown system:system device_owner.xml

reboot

Note : Before rebooting device, make sure that you installed the application, which you are trying to make device owner. If you will not do, you will get boot animation for infinite time.

Check this question How to make my app a device owner?

nkalra0123
  • 2,281
  • 16
  • 17
  • I want to run above mentioned command programmatically. These are all shell commands which needs to be run on terminal. So Anything that can be run in code is helpful. – Ravneet Singh Dec 31 '18 at 05:26
0

Create the xml file and copy from your code to the correct location /data/system/device_owner.xml and execute this code to set proper permission. and call reboot command

final String[] run_cmd = new String[]{"chown","system:system","/data/system/device_owner.xml"};
String reboot = "/system/bin/reboot";

execute(null,run_cmd);
execute(null,reboot);


 public void execute(Map<String, String> environvenmentVars, String[] cmd) {

            try {

                Process process = Runtime.getRuntime().exec(cmd);
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                StringBuffer output = new StringBuffer();

                char[] buffer = new char[4096];
                int read;

                while ((read = reader.read(buffer)) > 0) {
                    output.append(buffer, 0, read);
                }

                reader.close();

                process.waitFor();    
                BufferedReader reader2 = new BufferedReader(new InputStreamReader(process.getErrorStream()));    
                StringBuffer output2 = new StringBuffer();

                char[] buffer2 = new char[4096];
                int read2;

                while ((read2 = reader2.read(buffer2)) > 0) {
                    output2.append(buffer2, 0, read2);
                }

                reader.close();
                process.waitFor();
                }

            catch (Exception e)
            {

            }

        }
nkalra0123
  • 2,281
  • 16
  • 17