【问题标题】:su access androidsu访问安卓
【发布时间】:2012-07-03 13:06:00
【问题描述】:

我正在尝试制作一个文件浏览器(特别是我开始更改这个:http://android-er.blogspot.com.es/2010/01/implement-simple-file-explorer-in.html)。

我的问题是:如何访问并查看目录“/”中的文件?我发现那里正在寻找著名的:Runtime.getRuntime().exec("su");,但我只做那个超级用户授予管理员权限并停止工作。

我按照这里的步骤来根模拟器:How to get root access on Android emulator? 。反正我的手机已经root了,不能用。

这是代码。该应用程序在超级用户的列表中,但总是得到消息文件夹无法读取!

public class AndroidExplorer extends ListActivity {

private List<String> item = null;
private List<String> path = null;
private String root="/";
private TextView myPath;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    List<String> cmds = new ArrayList<String>();
    cmds.add("mount -o rw,remount /system");
    try {
        doCmds(cmds);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    myPath = (TextView)findViewById(R.id.path);
    getDir(root);
}

public void doCmds(List<String> cmds) throws Exception {
    Process process = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(process.getOutputStream());

    for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd+"\n");
    }

    os.writeBytes("exit\n");
    os.flush();
    os.close();

    process.waitFor();
} 

private void getDir(String dirPath)
{
 myPath.setText("Location: " + dirPath);
 item = new ArrayList<String>();
 path = new ArrayList<String>();
 File f = new File(dirPath);
 File[] files = f.listFiles();

 if(!dirPath.equals(root))
 {
  item.add(root);
  path.add(root);
  item.add("../");
  path.add(f.getParent());
 }
 for(int i=0; i < files.length; i++)
 {
   File file = files[i];
   path.add(file.getPath());
   if(file.isDirectory())
    item.add(file.getName() + "/");
   else
    item.add(file.getName());
 }
 ArrayAdapter<String> fileList =
  new ArrayAdapter<String>(this, R.layout.row, item);
 setListAdapter(fileList);
}

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  File file = new File(path.get(position));
  if (file.isDirectory())
  {
   if(file.canRead())
    getDir(path.get(position));
   else
   {
    new AlertDialog.Builder(this)
    .setIcon(R.drawable.ic_launcher)
    .setTitle("[" + file.getName() + "] folder can't be read!")
    .setPositiveButton("OK", 
      new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
       }
      }).show();
   }
  }
  else
  {
   new AlertDialog.Builder(this)
    .setIcon(R.drawable.ic_launcher)
    .setTitle("[" + file.getName() + "]")
    .setPositiveButton("OK", 
      new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
       }
      }).show();
  }
 }

}

【问题讨论】:

  • 您的应用程序无法以 root 身份运行,它只能使用可执行的辅助程序。
  • 感谢 Lukas,如果我的应用程序无法以管理员身份运行...我的应用程序如何读取和写入“/”中的文件?

标签: android root su


【解决方案1】:

反正我的手机已经root了,不能用。

首先,您需要一部可用的已植根手机。生根方法取决于您的手机型号,因此您需要谷歌并弄清楚。

成功root手机后,您将拥有超级用户权限,可以执行su命令。

您可以像这样检查您的手机是否正确植根(在 Windows 命令提示符下):

> adb shell
$ su

您应该能够看到根文件夹中的文件,例如:

$ cd /
$ ls -al

编辑

替换:

Runtime r = Runtime.getRuntime();
process = r.exec("su");

以下内容:

List<String> cmds = new List<String>();
cmds.add("mount -o rw,remount /system");
doCmds(cmds);

并添加如下函数:

public void doCmds(List<String> cmds) throws Exception {
    Process process = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(process.getOutputStream());

    for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd+"\n");
    }

    os.writeBytes("exit\n");
    os.flush();
    os.close();

    process.waitFor();
}  

【讨论】:

  • Caner 谢谢你的回答!!,对不起,我没有解释清楚。我有一部带有 Cyanogen mod 的手机,因此它是植根的。我在模拟器上安装了应用程序根资源管理器(v2.16)和超级用户并正常工作。我有 rootexplorer 的 root 访问权限,我可以读取和写入所有目录。我得到我的应用程序存储在超级用户的应用程序列表中(使用rootexplorer)我通过Runtime.getRuntime()得到了它。执行(“su”),但现在我无法读取、复制或删除任何文件。
  • 好的,当您尝试读取/复制/删除时遇到什么错误?试试“mount -o rw,remount /system”
  • 您好 Caner,我已经编辑了帖子。我把代码。流程总是按照“ if (file.canRead ()) ”的else方式进行。如果我删除该条件,我会强制关闭:“应用程序已意外停止。请重试。
  • 例如如果我用rootexplorer去“/data”,我可以看到里面的所有文件。但是,使用此应用程序时,我会收到警报“无法读取文件夹!”
  • 还是不行,继续提示“无法读取文件”。我编辑了第一篇文章的代码,所以你可以看到我已经改变并且我已经测试了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
相关资源
最近更新 更多