android 应用使用Root权限执行linux命令

时间:2021-04-21 07:38:25

要让Android应用使用Root权限,首先Android设备必须已经获得Root权限。之后可以通过下面的代码取得process对象。

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

Process对象包含可以取得输入输出流及错误流对象,使用OutputStream对象,可以向Linux写入命令,其中getInputStream将取得命令返回的数据,如果出错getErrorStream将返回错误信息。

public abstract OutputStream getOutputStream();
public abstract InputStream getInputStream();
public abstract InputStream getErrorStream();

需要注意的是,使用 cat 查看某些系统文件时,getInputStream 或 getErrorStream 读取数据时不返回,因此代码中最后使用 getOutputStream 在最后再加入一个 exit 命令后,再读取数据。

源码如下

package com.john.wifipwd;

import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List; public class RootCommand { public interface OnCommandCallback {
void onCommandResult(List<String> cmds, String result); void onCommandErrorResult(List<String> cmds, String errorMsg);
} public static final int MESSAGE_COMMAND = 1001; private HandlerThread mHandlerThread; private WorkerHandler mWorkerHandler; private Handler mUIHandler; private OnCommandCallback mOnCommandCallback; public RootCommand() {
this(null, null);
} public RootCommand(OnCommandCallback callback) {
this(null, callback);
} public RootCommand(Handler handler, OnCommandCallback callback) {
mUIHandler = handler;
mOnCommandCallback = callback; mHandlerThread = new HandlerThread("su-Thread");
mHandlerThread.start();
mWorkerHandler = new WorkerHandler(mHandlerThread.getLooper());
} public void addCommand(String cmd) {
if (cmd != null) {
ArrayList<String> obj = new ArrayList<>();
obj.add(cmd);
Message msg = Message.obtain();
msg.what = MESSAGE_COMMAND;
msg.obj = obj;
mWorkerHandler.sendMessage(msg);
}
} public void addCommands(List<String> cmds) {
if (cmds != null) {
ArrayList<String> obj = new ArrayList<>();
obj.addAll(cmds);
Message msg = Message.obtain();
msg.what = MESSAGE_COMMAND;
msg.obj = obj;
mWorkerHandler.sendMessage(msg);
}
} public void quit() {
mWorkerHandler.removeMessages(MESSAGE_COMMAND);
mWorkerHandler.quit();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mHandlerThread.quitSafely();
} else {
mHandlerThread.quit();
}
} private class WorkerHandler extends Handler { private boolean mIsQuit; public WorkerHandler(Looper looper) {
super(looper);
} public void quit() {
mIsQuit = true;
} @Override
@SuppressWarnings("unchecked")
public void handleMessage(Message msg) {
super.handleMessage(msg); switch (msg.what) {
case MESSAGE_COMMAND: {
List<String> cmds = (List<String>) msg.obj;
String line;
StringBuilder result = new StringBuilder();
boolean isError = false; Process process = null;
BufferedReader error = null;
BufferedReader reader = null;
BufferedWriter writer = null; try {
process = Runtime.getRuntime().exec("su root");
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
reader = new BufferedReader(new InputStreamReader(process.getInputStream())); Dlog.d("--> call write");
for (String command : cmds) {
Dlog.d("handle message:" + command);
writer.write(command + " \n");
}
writer.write("exit \n");
writer.flush();
process.waitFor(); Dlog.d("--> error read");
while (!mIsQuit && (line = error.readLine()) != null) {
result.append(line).append("\n");
isError = true;
}
Dlog.d("--> isError = " + isError);
if (!isError) {
Dlog.d("--> Reader read");
while (!mIsQuit && (line = reader.readLine()) != null) {
result.append(line).append("\n");
}
} if (!mIsQuit) {
if (!isError) {
if (mUIHandler != null) {
mUIHandler.post(new ResultRunnable(cmds, result.toString()));
} else if (mOnCommandCallback != null) {
mOnCommandCallback.onCommandResult(cmds, result.toString());
}
} else {
if (mUIHandler != null) {
mUIHandler.post(new ErrorResultRunnable(cmds, result.toString()));
} else if (mOnCommandCallback != null) {
mOnCommandCallback.onCommandErrorResult(cmds, result.toString());
}
}
}
Dlog.d("handle message end!");
} catch (Exception e) {
Dlog.e("", e);
} finally {
close(writer);
close(error);
close(reader);
if (process != null) {
process.destroy();
}
}
break;
} default:
break;
}
} private void close(Closeable stream) {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
Dlog.e("", e);
}
}
} private class ResultRunnable implements Runnable { private String mResult;
private List<String> mCmds; public ResultRunnable(List<String> cmds, String result) {
mCmds = cmds;
mResult = result;
} @Override
public void run() {
if (mOnCommandCallback != null) {
mOnCommandCallback.onCommandResult(mCmds, mResult);
}
}
} private class ErrorResultRunnable implements Runnable { private String mResult;
private List<String> mCmds; public ErrorResultRunnable(List<String> cmds, String result) {
mCmds = cmds;
mResult = result;
} @Override
public void run() {
if (mOnCommandCallback != null) {
mOnCommandCallback.onCommandErrorResult(mCmds, mResult);
}
}
}
}

以查看连接过的wifi密码为例,下面的代码演示使用方法

package com.john.wifipwd;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; public class WiFiAcy extends AppCompatActivity implements RootCommand.OnCommandCallback { private RootCommand mRootCommand; private ArrayList<String> mCmds = new ArrayList<>(); private List<WpaInfo> mWpaInfos = new ArrayList<>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wpa_pwd_acy); mRootCommand = new RootCommand(new Handler(), this);
sendCmd("cat /data/misc/wifi/wpa_supplicant.conf");
} @Override
protected void onDestroy() {
super.onDestroy();
mRootCommand.quit();
} @Override
public void onCommandResult(List<String> cmds, String result) {
Dlog.d("[onCommandResult]");
if (cmds != null && cmds.equals(mCmds)) {
for (String cmd : cmds) {
Dlog.d("--> " + cmd);
}
Dlog.d("" + result); if (result != null) {
mWpaInfos.clear();
parseWpa(mWpaInfos, result);
createNetWorkViews();
}
}
} @Override
public void onCommandErrorResult(List<String> cmds, String errorMsg) {
Dlog.d("[onCommandErrorResult]");
if (cmds != null && cmds.equals(mCmds)) {
for (String cmd : cmds) {
Dlog.d("--> " + cmd);
}
Dlog.d("" + errorMsg);
}
Toast.makeText(this, errorMsg, Toast.LENGTH_LONG).show();
} private void sendCmd(String cmd) {
mCmds.clear();
mCmds.add(cmd);
mRootCommand.addCommands(mCmds);
} private void createNetWorkViews() {
LayoutInflater localLayoutInflater = getLayoutInflater();
ViewGroup localViewGroup = (ViewGroup) findViewById(R.id.ll_wifi_pwds);
if (localViewGroup == null)
return;
localViewGroup.removeAllViews();
Iterator localIterator = mWpaInfos.iterator();
TextView textView;
while (true) {
if (!localIterator.hasNext())
return;
WpaInfo localNetWorkInfo = (WpaInfo) localIterator.next();
Dlog.d(localNetWorkInfo.toString());
View localView = localLayoutInflater.inflate(R.layout.wpa_pwd_item, localViewGroup, false);
textView = (TextView) localView.findViewById(R.id.ssid);
textView.setText(getString(R.string.ssid_tag) + localNetWorkInfo.getSsid());
textView = (TextView) localView.findViewById(R.id.psk);
textView.setText(getString(R.string.psk_tag) + localNetWorkInfo.getPsk());
textView = (TextView) localView.findViewById(R.id.keyMgmt);
textView.setText(getString(R.string.keyMgmt_tag) + localNetWorkInfo.getKeyMgmt());
textView = (TextView) localView.findViewById(R.id.priority);
textView.setText(getString(R.string.priority_tag) + localNetWorkInfo.getPriority());
localViewGroup.addView(localView);
}
} private void parseWpa(List<WpaInfo> outList, String content) {
String[] array = content.split("\n");
WpaInfo wpaInfo = null;
for (String line : array) {
line = line.trim();
if (line.startsWith(WpaInfo.NETWORK_PREFIX)) {
wpaInfo = new WpaInfo();
outList.add(wpaInfo);
} else if (wpaInfo != null) {
if (line.startsWith(WpaInfo.SSID_PREFIX)) {
line = line.substring(WpaInfo.SSID_PREFIX.length());
line = line.replaceAll("\"", "");
wpaInfo.setSsid(line);
} else if (line.startsWith(WpaInfo.PSK_PREFIX)) {
line = line.substring(WpaInfo.PSK_PREFIX.length());
line = line.replaceAll("\"", "");
wpaInfo.setPsk(line);
} else if (line.startsWith(WpaInfo.KEY_MGMT_PREFIX)) {
line = line.substring(WpaInfo.KEY_MGMT_PREFIX.length());
wpaInfo.setKeyMgmt(line);
} else if (line.startsWith(WpaInfo.PRIORITY_PREFIX)) {
line = line.substring(WpaInfo.PRIORITY_PREFIX.length());
wpaInfo.setPriority(line);
Dlog.d("" + wpaInfo);
}
}
}
}
}
package com.john.wifipwd;

public class WpaInfo {

    public static final String NETWORK_PREFIX = "network={";
public static final String SSID_PREFIX = "ssid=";
public static final String PSK_PREFIX = "psk=";
public static final String KEY_MGMT_PREFIX = "key_mgmt=";
public static final String PRIORITY_PREFIX = "priority="; private String ssid;
private String psk;
private String keyMgmt;
private String priority; public WpaInfo() {
} public WpaInfo(String ssid, String psk, String keyMgmt, String priority) {
this.ssid = ssid;
this.psk = psk;
this.keyMgmt = keyMgmt;
this.priority = priority;
} public String getSsid() {
return ssid;
} public void setSsid(String ssid) {
this.ssid = ssid;
} public String getPsk() {
return psk;
} public void setPsk(String psk) {
this.psk = psk;
} public String getKeyMgmt() {
return keyMgmt;
} public void setKeyMgmt(String keyMgmt) {
this.keyMgmt = keyMgmt;
} public String getPriority() {
return priority;
} public void setPriority(String priority) {
this.priority = priority;
} @Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false; WpaInfo wpaInfo = (WpaInfo) o; if (ssid != null ? !ssid.equals(wpaInfo.ssid) : wpaInfo.ssid != null)
return false;
if (psk != null ? !psk.equals(wpaInfo.psk) : wpaInfo.psk != null)
return false;
if (keyMgmt != null ? !keyMgmt.equals(wpaInfo.keyMgmt) : wpaInfo.keyMgmt != null)
return false;
return priority != null ? priority.equals(wpaInfo.priority) : wpaInfo.priority == null;
} @Override
public int hashCode() {
int result = ssid != null ? ssid.hashCode() : 0;
result = 31 * result + (psk != null ? psk.hashCode() : 0);
result = 31 * result + (keyMgmt != null ? keyMgmt.hashCode() : 0);
result = 31 * result + (priority != null ? priority.hashCode() : 0);
return result;
} @Override
public String toString() {
return "WpaInfo{" +
"ssid='" + ssid + '\'' +
", psk='" + psk + '\'' +
", keyMgmt='" + keyMgmt + '\'' +
", priority='" + priority + '\'' +
'}';
}
}