通过在Android中C $ C $Ç执行命令 [英] Execute commands via C code in Android

查看:167
本文介绍了通过在Android中C $ C $Ç执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行的命令,截图亚行的shell /系统/斌/撷取画面-p /sdcard/img.png进入C.我在寻找相同和我另一个命令一个解决方案,我修改了命令

  EXECL(/系统/斌/撷取画面,-p,存储/ sdcard0 / screenShot.png(字符*)NULL);

但是当我运行上述命令我的应用程序和呼叫的方法,应用程序得到崩溃。

我应该如何修改 /系统/斌/撷取画面-p /sdcard/img.png 命令,从C code运行。


汤姆回答后更新

应用是越来越再次关闭,这里是日志

  11月6日至21日:52:01.488:我/的WindowState(279):WIN死亡:窗口{40fed2c0 U0 com.mytest.ndktestapplication / com.mytest.ndktestapplication.MainActivity}
11月6日至21日:52:01.498:I / ActivityManager(279):进程com.mytest.ndktestapplication(PID 7745)已经死亡。
11月6日至21日:52:01.498:W / ActivityManager(279):强制删除ActivityRecord {40ea9ab8 U0 com.mytest.ndktestapplication / .MainActivity}:应用程序死了,没有保存的状态


解决方案

这是EXEC的预期效果()系列的功能。

他们所做的就是替换当前程序的指定的一个。所以,再见应用程序。

要避免这种情况,你首先需要调用fork(),然后调用exec()只在孩子,像这样:

 如果(!叉()){
    // fork()的返回零,所以我们是在孩子
    EXECL ...
}

您可能还调用exec函数之前做一些清理工作。

注意但是,你将无法从大多数设备的应用程序采取截图,随着应用程序code缺乏这样做的权限的用户ID下运行。但是我依稀记得,有一些的地方运设备没有此功能的权限检查狭窄的时期,所以它可能在这些工作。

I want to execute the screenshot command "adb shell /system/bin/screencap -p /sdcard/img.png" into C. I was searching for the same and I got a solution for another command and I modified the command as

execl("/system/bin/screencap", "-p", "storage/sdcard0/screenShot.png", (char *)NULL);

but when I run my application and call method of above command, application gets crash.

How should I modify the "/system/bin/screencap -p /sdcard/img.png" command to run from C code.


Update after tom answer

Application is getting closed again and here is log

06-21 11:52:01.488: I/WindowState(279): WIN DEATH: Window{40fed2c0 u0 com.mytest.ndktestapplication/com.mytest.ndktestapplication.MainActivity}
06-21 11:52:01.498: I/ActivityManager(279): Process com.mytest.ndktestapplication (pid 7745) has died.
06-21 11:52:01.498: W/ActivityManager(279): Force removing ActivityRecord{40ea9ab8 u0 com.mytest.ndktestapplication/.MainActivity}: app died, no saved state

解决方案

This is the expected result of exec() family functions.

What they do is replace the current program with the specified one. So bye-bye app.

To avoid that you would first need to call fork(), and then call exec() only in the child, something like this:

if (!fork()) {
    // fork() returned zero, so we are in the child
    execl...
}

You might also have to do some cleanup before calling the exec function.

Note however that you will not be able to take a screenshot from an app on most devices, as application code runs under a user id which lacks the permission to do so. But I seem to recall that there was a narrow period where some devices shipped without permission checks on this functionality, so it might work on those.

这篇关于通过在Android中C $ C $Ç执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆