简体   繁体   中英

Run commands Android Terminal Emulator. Install apk by code

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!

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

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!

I have a doubt that when you are executing a command its not a ADB Shell its your application's shell environment.

From code: (If you want to do it by Intent then)

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path+"/<application_name>.apk")), "application/vnd.android.package-archive");
startActivity(intent);

And this is the permissions..

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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