編輯:關於Android編程
public static void execCommand(String[] commands, boolean isRoot,
ShellCommandListener listener) throws IOException,
InterruptedException, TimeoutException {
int exitCode = -1;
CommandResult result = null;
if (commands == null || commands.length == 0) {
result = new CommandResult(exitCode, null, null);
listener.onCommandFinished(result);
}
Process process = null;
BufferedReader successReader = null;
BufferedReader errorReader = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
process = Runtime.getRuntime().exec(isRoot ? "su" : "sh");
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
}
// donnot use os.writeBytes(commmand), avoid chinese charset
// error
os.write(command.getBytes());
os.writeBytes("\n");
os.flush();
}
os.writeBytes("exit\n");
os.flush();
exitCode = process.waitFor();
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
errorReader = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String s = null;
while ((s = successReader.readLine()) != null) {
successMsg.append(s + "\n");
}
while ((s = errorReader.readLine()) != null) {
errorMsg.append(s + "\n");
}
if (exitCode == -257) {
throw new TimeoutException();
}
try {
if (os != null) {
os.close();
}
if (successReader != null) {
successReader.close();
}
if (errorReader != null) {
errorReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
result = new CommandResult(exitCode, successMsg == null ? null
: successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
listener.onCommandFinished(result);
}
/**
* result of command,
*
* @author Trinea 2013-5-16
*/
public static class CommandResult {
/** result of command **/
public int exitCode;
/** success message of command result **/
public String successMsg;
/** error message of command result **/
public String errorMsg;
public CommandResult(int result) {
this.exitCode = result;
}
public CommandResult(int result, String successMsg, String errorMsg) {
this.exitCode = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
@Override
public String toString() {
return "exitCode=" + exitCode + "; successMsg=" + successMsg
+ "; errorMsg=" + errorMsg;
}
}
public interface ShellCommandListener {
public void onCommandFinished(CommandResult result);
}
Android Material Design新UI控件使用大全 三
序言在我們對NavigationView側滑,TextInputLayout輸入框,Snackbar彈出提示框,FloatingActionBar圓形button,Tab
AIDL/IPC Android AIDL/IPC 進程通信機制——超詳細講解及用法案例剖析(播放器)
首先引申下AIDL,什麼是AIDL呢?IPC? ------ Designing a Remote Interface Using AIDL 通常情況下,我們在同一進程內
android實現藍牙文件發送,支持多種機型
最近項目上需要實現藍牙傳輸apk的一個功能,能夠搜索周圍的藍牙手機並分享文件。從需求上講android手機自帶的藍牙傳輸模塊就可以滿足需要了,實現也很簡單。不過讓人頭疼的
2.3.1 存儲數據到data目錄中
當應用安裝到Android後,系統會根據每個應用的包名創建一個/data/data/包名/的文件夾,訪問自己包名下的目錄是不需要權限的,並且Android已經提供了非常簡