简体   繁体   English

以编程方式使用命令使应用程序包成为设备所有者

[英]Making app package as device owner using command programmatically

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 ShellExecuter代码段

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 MyDeviceAdminReceiver片段

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 device_admin_reciever代码段

<?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. 我想以编程方式使用命令或任何其他建议的方式使我的植根设备所有者成为我的应用程序包的所有者。

If you're root on your device, you can follow this method to become device owner. 如果您是设备的root用户,则可以按照此方法成为设备所有者。

First, create a file device_owner.xml with following content: 首先,创建具有以下内容的文件device_owner.xml:

<?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? 检查此问题如何使我的应用成为设备所有者?

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. 创建xml文件,然后从您的代码复制到正确的位置/data/system/device_owner.xml,然后执行此代码以设置适当的权限。 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)
            {

            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM