我正在编写一个简单的应用程序,使用 react native 来自动完成一些文件操作。我尝试过像强文本 react-native-fs这样的软件包,但我发现有些文件/文件夹无法使用
ContentValues
尝试列出该文件夹中的文件会引发异常。
但是,可以使用 adb shell 中的 ls 命令显示这些文件。所以我想知道是否有一种方法可以在反应原生中运行shell命令,就像我们在java / python中进行一些系统调用一样?
由于
答案 0 :(得分:1)
您可以轻松地从Java运行shell命令,并使用React Native的Native Modules从React Native调用它。您可以在此处找到更多信息:
https://facebook.github.io/react-native/docs/0.60/native-modules-android#callbacks
对于您的Java命令,您想使用类似的东西:
String command = "ls " + path; // Where path is your desired path.
Runtime runtime = Runtime.getRuntime();
Process result = runtime.exec( command );
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( result.getInputStream() ) );
String[] parts = bufferedReader.readLine().split( "\\s+" );
答案 1 :(得分:0)
@flyskywhy/react-native-android-shell 即使使用 root 命令也能很好地工作,例如
AndroidShell.executeCommand('su -c ifconfig eth0 down; su -c ifconfig eth0 hw ether 19:21:19:49:20:21; su -c ifconfig eth0 up', (result) => {
console.log(result)
});