【发布时间】:2017-08-11 09:59:48
【问题描述】:
我正在编写一个 C++ 代码来在 android 设备中执行“top”命令。这是我使用的代码。
using namespace std;
int main()
{
char buffer[1024];
string result;
system("top -n 1 | head -n 4 | tail -n 3");
FILE *memcpu= popen("top -n 1 | head -n 4 | tail -n 3","r");
while (!feof(memcpu)) {
if (fgets(buffer, 1024, memcpu) != NULL)
result+=buffer;
}
cout<<"result you need\n"<<result;
}
我想在 adb 设备中运行这个文件。因此我使用命令构建程序
arm-linux-gnueabi-g++ -static -march=armv7-a name.cpp -o test
当我运行程序时,字符串result是空的。
我通过在程序中包含system("top -n 1"); 行来测试程序。但我没有从 adb shell 得到任何输出(空字符串)。
我使用 g++ 构建了相同的程序并在 linux pc 中运行。那时我得到了正确的输出。我没有从 android 设备的 adb shell 中获得所需输出的原因可能是什么?
【问题讨论】:
-
您是使用
JNI还是其他方式运行此c++代码? -
我没有使用 JNI。基本上我只是将此程序构建为设备兼容的可执行文件,并使用“adb push”命令将此可执行文件放入 system/bin 文件夹。然后我进入 adb shell 并从 'bin' 执行程序。
-
您的测试设备上是否提供所有 3 个程序(顶部、头部和尾部)?
-
是的,他们是。我可以从 shell 执行这些命令。