Run commands Android Terminal Emulator. Install apk by code

54,650

Solution 1

try this :

su -c "pm install /mnt/sdcard/app.apk" root

But i think, it doesn't work if you have no root your phone

Solution 2

I found the solution.

    Process p;  
    try {  
            // Preform su to get root privledges  
            p = Runtime.getRuntime().exec("su");   

            // Attempt to write a file to a root-only  
            DataOutputStream os = new DataOutputStream(p.getOutputStream());  
            os.writeBytes(**any command**+"\n");  

            // Close the terminal  
            os.writeBytes("exit\n");  
            os.flush();  
            try {  
                    p.waitFor();
                    if (p.exitValue() != 255) {  
                            // Sucess :-)
                    }  
                    else {  
                           // Fail
                    }  

            } catch (InterruptedException e) {  
                    // Fail
            }  
    } catch (IOException e) {  
            // Fail
    } 

Thanks all!

Share:
54,650
Ricmcm
Author by

Ricmcm

Updated on July 09, 2022

Comments

  • Ricmcm
    Ricmcm almost 2 years

    I'm trying to run commands on the Android Terminal Emulator. I have my device rooted and I installed superuser.apk.

    I'm doing it like this:

        try {
            Process process = Runtime.getRuntime().exec(cmd);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = bufferedReader.readLine();
            while ( line != null ) {
                Log.i("SHELL", line);
                line = bufferedReader.readLine();
            }
        } catch ( Exception e) {
            e.printStackTrace();
        }
    

    And also well:

        try {
            Process process = Runtime.getRuntime().exec(<b>cmd</b>*);
            process.waitFor();
        } catch ( Exception e ){
            e.printStackTrace();
        }
    

    I'm trying to install a. Apk by code. I tried the following:

    cmd => pm install /mnt/sdcard/app.apk Without any results.

    cmd => su -c "pm install /mnt/sdcard/app.apk" With single quotes, double quoutes and no quotes. Result:

        I/SHELL(1432): Usage: su [options] [LOGIN]
    
        I/SHELL(1432): Options:
        I/SHELL(1432):   -c, --command COMMAND         pass COMMAND to the invoked shell
        I/SHELL(1432):   -h, --help                    display this help message and exit
        I/SHELL(1432):   -, -l, --login                make the shell a login shell
        I/SHELL(1432):   -s, --shell SHELL             use SHELL instead of the default in passwd
        I/SHELL(1432):   -v, --version                 display version number and exit
        I/SHELL(1432):   -V                            display version code and exit. this is
        I/SHELL(1432):                                 used almost exclusively by Superuser.apk
    

    Other commands like ls runs fine.

    How I can install an apk located in /mnt/sdcard?

    Thanks!