运行命令Android Terminal Emulator。按代码安装apk

时间:2012-07-19 11:20:12

标签: android install apk terminal-emulator

我正在尝试在Android终端模拟器上运行命令。我的设备已植根,我安装了 superuser.apk

我这样做:

    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();
    }

而且还很好:

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

我正在尝试安装一个。 Apk代码。我尝试了以下方法:

cmd =&gt; pm install /mnt/sdcard/app.apk 没有任何结果。

cmd =&gt; su -c "pm install /mnt/sdcard/app.apk" 单引号,双引号和无引号。结果:

    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

ls 等其他命令运行正常。

如何在/ mnt / sdcard中安装apk?

谢谢!

3 个答案:

答案 0 :(得分:4)

试试这个:

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

但我认为,如果你的手机没有root,它就无效了

答案 1 :(得分:3)

我找到了解决方案。

    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
    } 

全部谢谢!

答案 2 :(得分:0)

我怀疑当你执行命令时,它不是ADB Shell而是你的应用程序的shell环境。

从代码:(如果你想通过Intent那样做)

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

这是权限..

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
相关问题