*/
public static class CommandResult {
public int result = -1;
public String errorMsg;
public String successMsg;
}
/**
-
执行命令—单条
-
@param command
-
@param isRoot
-
@return
*/
public static CommandResult execCommand(String command, boolean isRoot) {
String[] commands = {command};
return execCommand(commands, isRoot);
}
/**
-
执行命令-多条
-
@param commands
-
@param isRoot
-
@return
*/
public static CommandResult execCommand(String[] commands, boolean isRoot) {
CommandResult commandResult = new CommandResult();
if (commands == null || commands.length == 0) return commandResult;
Process process = null;
DataOutputStream os = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
try {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command != null) {
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
}
os.writeBytes(COMMAND_EXIT);
os.flush();
commandResult.result = process.waitFor();
//获取错误信息
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) successMsg.append(s);
while ((s = errorResult.readLine()) != null) errorMsg.append(s);
commandResult.successMsg = successMsg.toString();
commandResult.errorMsg = errorMsg.toString();
Log.i(TAG, commandResult.result + " | " + commandResult.successMsg
- " | " + commandResult.errorMsg);
} catch (IOException e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e(TAG, errmsg);
} else {
e.printStackTrace();
}
} catch (Exception e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e(TAG, errmsg);
} else {
e.printStackTrace();
}
} finally {
try {
if (os != null) os.close();
if (successResult != null) successResult.close();
if (errorResult != null) errorResult.close();
} catch (IOException e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e(TAG, errmsg);
} else {
e.printStackTrace();
}
}
if (process != null) process.destroy();
}
return commandResult;
}
}
- gpio读写操作
package cn.hxg.androidgpiotest;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity e
xtends AppCompatActivity {
private String[] gpioShell = {“cd /sys/class/gpio”, “echo 3 > export”,
“cd gpio3”, “echo in >direction”, “chmod 777 value”};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
if (FileUtils.fileIsExists(“/sys/class/gpio/gpio3”)) {
CommandExecution.CommandResult commandResult = CommandExecution.execCommand(gpioShell, true);
Log.e(“huang”, commandResult.result + " | " + commandResult.successMsg
- " | " + commandResult.errorMsg);
while (true) {
SystemClock.sleep(1000);
String s = FileUtils.ReadTxtFile(“/sys/class/gpio/gpio3/value”);
Log.i(“huang”, “s===>” + s);
}
}
}
}).start();
}
}
- 文件操作类
package cn.hxg.androidgpiotest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class FileUtils {
//判断文件是否存在
public static boolean fileIsExists(String filePath) {
try {
File f = new File(filePath);
if (!f.exists()) {
return false;
}
} catch (Exception e) {
return false;
}
return true;
}
/**
-
读取文件
-
@param strFilePath
-
@return
*/
public static String ReadTxtFile(String strFilePath) {
String path = strFilePath;
String content = “”; //文件内容字符串
//打开文件
File file = new File(path);
if (!file.exists()) { //文件不存在
return content;
}
//如果path是传递过来的参数,可以做一个非目录的判断
if (!file.isDirectory()) {
try {
InputStream instream = new FileInputStream(file);
if (instream != null) {
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
//分行读取
while ((line = buffreader.readLine()) != null) {
content += line + “\n”;
}
最后
小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
如果你需要这些资料, ⬅ 专栏获取
能够帮助到想自学提升又不知道该从何学起的朋友。**
[外链图片转存中…(img-jjPXPsYC-1719165308917)]
[外链图片转存中…(img-FlhSpexF-1719165308918)]
[外链图片转存中…(img-VD2CbduA-1719165308919)]
[外链图片转存中…(img-lthb3dJI-1719165308920)]
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
如果你需要这些资料, ⬅ 专栏获取