Android using Super User Permissions ? allowing access

前端 未结 3 1300
醉梦人生
醉梦人生 2020-12-05 09:10

im trying to find the method behind having superuser access on android devices. basically i want my app to be able to remove system files, and so asking the user if its ok a

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 09:31

    If the device is rooted, you can use the following code to gain access to a root shell:

        try
        {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream outputStream = new DataOutputStream(process.getOutputStream()); 
            DataInputStream inputStream = new DataInputStream(process.getInputStream());
    
            outputStream.writeBytes(command + "\n");
            outputStream.flush();
    
            outputStream.writeBytes("exit\n"); 
            outputStream.flush(); 
            process.waitFor();
        }
        catch (IOException e)
        {
            throw new Exception(e);
        }
        catch (InterruptedException e)
        {
            throw new Exception(e);
        }
    

    Where "command" is the command that you want to execute.

提交回复
热议问题