求大家帮助!eclipse 两段简单的代码合并有关问题
求大家帮助!eclipse 两段简单的代码合并问题。
我写了一个app, 目的是想利用app封装并调用一个可执行程序。但是在调用可执行程序时显示没有root权限,手机已经root。因此认为是不是让app 获取root权限即可?
封装并调用可执行程序代码如下:
package com.test.android.exe;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private String exe_path = "data/data/com.test.android.exe/hello";
private File exe_file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
copyBigDataToSD(exe_path);
exe_file = new File(exe_path);
exe_file.setExecutable(true, true);
execCmd(exe_path);
} catch (IOException e1) {
e1.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void execCmd(String cmd) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmd);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while (null != (line = br.readLine())) {
Log.e("########", line);
}
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void copyBigDataToSD(String strOutFileName) throws IOException
{
InputStream myInput;
OutputStream myOutput = new FileOutputStream(strOutFileName);
myInput = this.getAssets().open("hello");
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while(length > 0)
{
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}
myOutput.flush();
myInput.close();
myOutput.close();
}
}
获取root权限代码如下:(网上找的)
public boolean getRootPermission(String pkgCodePath) {
Log.d(TAG, "pkgCodePath=" + pkgCodePath);
Process process = null;
DataOutputStream os = null;
try {
String cmd = "chmod 777 " + pkgCodePath;
process = Runtime.getRuntime().exec("su"); //切换到root帐号
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Toast.makeText(this, "root error!" + pkgCodePath, Toast.LENGTH_SHORT).show();
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
Toast.makeText(this, "root success!" + pkgCodePath, Toast.LENGTH_SHORT).show();
return true;
}
请问把后一段代码能加入到前面代码吗?哪个大哥能帮我加一下啊,我真的不懂代码,谢谢。跪求啊,弄了好几天了。
------解决思路----------------------
把代码贴到你的代码里,然后在需要的地方调用getRootPermission(pkgCodePath)
我写了一个app, 目的是想利用app封装并调用一个可执行程序。但是在调用可执行程序时显示没有root权限,手机已经root。因此认为是不是让app 获取root权限即可?
封装并调用可执行程序代码如下:
package com.test.android.exe;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private String exe_path = "data/data/com.test.android.exe/hello";
private File exe_file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
copyBigDataToSD(exe_path);
exe_file = new File(exe_path);
exe_file.setExecutable(true, true);
execCmd(exe_path);
} catch (IOException e1) {
e1.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void execCmd(String cmd) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmd);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while (null != (line = br.readLine())) {
Log.e("########", line);
}
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void copyBigDataToSD(String strOutFileName) throws IOException
{
InputStream myInput;
OutputStream myOutput = new FileOutputStream(strOutFileName);
myInput = this.getAssets().open("hello");
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while(length > 0)
{
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}
myOutput.flush();
myInput.close();
myOutput.close();
}
}
获取root权限代码如下:(网上找的)
public boolean getRootPermission(String pkgCodePath) {
Log.d(TAG, "pkgCodePath=" + pkgCodePath);
Process process = null;
DataOutputStream os = null;
try {
String cmd = "chmod 777 " + pkgCodePath;
process = Runtime.getRuntime().exec("su"); //切换到root帐号
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Toast.makeText(this, "root error!" + pkgCodePath, Toast.LENGTH_SHORT).show();
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
Toast.makeText(this, "root success!" + pkgCodePath, Toast.LENGTH_SHORT).show();
return true;
}
请问把后一段代码能加入到前面代码吗?哪个大哥能帮我加一下啊,我真的不懂代码,谢谢。跪求啊,弄了好几天了。
------解决思路----------------------
把代码贴到你的代码里,然后在需要的地方调用getRootPermission(pkgCodePath)