首先放源代码:
package com.main.app;import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.TextView;public class RootPermissionActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try {
                                TextView suCommandMessage = (TextView)findViewById(R.id.suCommandMessage);
                                //建立子程序 su
Process p = Runtime.getRuntime().exec("su");
                                //向子程序写入错误代码代码
DataOutputStream outputStream = new DataOutputStream(p.getOutputStream());
outputStream.writeBytes("touchasd /m\n");
                                //读取错误信息
                                BufferedReader d = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String sb = "";
String msg = "";
while((msg  = d.readLine()) != null) {
        sb = sb + msg;
}
//将错误信息显示在TextView上面
suCommandMessage.setText(sb);
                                //退出子程序
outputStream.writeBytes("exit \n");
                                //等待子程序完成
p.waitFor();
} catch(IOException e) {
new AlertDialog.Builder(this)
   .setTitle("Exception")
   .setMessage("IOException: " + e)
   .setPositiveButton("OK", null)
   .show();
} catch(InterruptedException e) {
new AlertDialog.Builder(this)
   .setTitle("Exception")
   .setMessage("InterruptedException: " + e)
   .setPositiveButton("OK", null)
   .show();
}
    }
}
本程序的作用是申请最高权限,然后建立子程序su,写入错误代码touchasd /m\n,接收错误信息并输出到TextView中,申请最高权限(没有Root的话,大家可以把其中的"su"改成 "sh"进行调试)一切正常,然后就没有反应了,调试发现,到while那个循环时,走了两遍就突然停下来了,不知道为什么,就像是Eclipse以为程序正常结束了一样,后面的代码都没有执行

解决方案 »

  1.   

    waitFor() 
              导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。
      

  2.   

    调试的时候,进入while语句后,第一次循环,msg值为空(好像不是null,因为如果是null的话就会跳出循环了),第二次循环,msg值为错误信息: touchasd: not found,这正是我想要输出的错误信息,第三次循环,刚进入while((msg  = d.readLine()) != null)后,再走一步,就没有反应了,就像整个程序正常结束了一样,如果像你所说的是因为waitfor的原因的话,那至少也得走到waitfor ()那一步吧,如果有兴趣的话,可以把Process p = Runtime.getRuntime().exec("su");中的su改成sh,可以在虚拟机里测试,我刚才在虚拟机里面试了一下,也是这种情况
      

  3.   

    是不是需要chmod?我对linux下的东西不太了解
      

  4.   

    已经是su最高级别了,应该不是chmod的原因了