首先放源代码:
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以为程序正常结束了一样,后面的代码都没有执行
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以为程序正常结束了一样,后面的代码都没有执行
解决方案 »
- opengl es怎样绑定自定义view
- 求解答,什么是Android Framwork开发
- popupWindow获取焦点后,parent如何响应点击事件
- 急急急,为什么资源图片不能填满tab?
- 带左右滑动的选项卡点击进入,返回进入指定的选项问题
- 关于Scrollview滑动如何跟Gridview一样快速
- android应用添加分享到新浪微博
- android关于html5
- ======== 小白问题,多指教 ============
- 求大神告知Android Studio 初次运行无法成功安装Gradle的解决方法
- 求教:android webview设置的锚点在第一次加载html时不起作用,??急!!
- 关于如何记录阅读进度,求点拨。标题要长…………一晚上过去了,怎么没有人回复我一下啊。
导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。