从Android程序运行shell命令 [英] Run shell commands from android program

查看:469
本文介绍了从Android程序运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被问这里之前,但所提供的解决方案并不working..I想显示的/数据/达尔维克缓存文件夹中的内容。我知道,要做到这一点,我们需要变得苏。我甚至这样做,但我仍然感到无法执行shell命令。

This question has been asked here before but the solutions provided are not working..I am trying to display the contents of /data/dalvik-cache folder. I know that to do this we need to become su. I even did that but still i am unable to execute a shell command..

package org.linuxconfidg.Example2;

import android.app.Activity;
import android.widget.*;
import android.os.Bundle;
import java.io.*;
public class Example2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String lsreturn=myFunLs();
        TextView tv=new TextView(this);
        tv.setText("Hello Sindhu !! Try to get it \n"+lsreturn);
        setContentView(tv);
    }

    public String myFunLs()
    {

        try {
            // Executes the command.
            Process process;
            process = Runtime.getRuntime().exec("/system/bin/su");
            process = Runtime.getRuntime().exec("/system/bin/ls /data/dalvik-cache > /data/local");
            pr
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();

            // Waits for the command to finish.
            process.waitFor();

            return output.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}

任何人都可以请帮我找到了如何在Android应用程序上运行Linux的命令。我测试这个程序在我的模拟器是defaultly植根

Can anyone please help me out in finding out how to run linux commands in android application. I am testing this app in my emulator which is defaultly rooted

推荐答案

在模拟器上你不能简单地运行素,有默认情况下没有root访问权限。您需要安装'苏'计划,以及在SuperUser.apk,你就会有这样每次启动模拟器,除非使用快照的时间做。

You can't simply run 'su' on the emulator, there's no root access by default. You'll need to install the 'su' program as well as the SuperUser.apk, and you'll have to do this each time you start the emulator unless using snapshots.

更多的信息和链接到你需要的文件可以发现<一href="http://stackoverflow.com/questions/5687082/obtain-root-access-via-su-on-the-android-emulator">here在SO以及这篇博客通过罗素·戴维斯

More information and links to the files you need can be found here on SO as well as this blog post by Russell Davis

这篇关于从Android程序运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆