(1)首先把execmd.sh打包到/system/bin目录下,具体可以仿照\device\amlogic\g18ref\g18ref.mk里的
# modify by Jet for Guangdong Mobile
PRODUCT_COPY_FILES += $(LOCAL_PATH)/execmd.sh:system/bin/execmd.sh
(2)在\device\amlogic\g18ref\修改init.rc,增加对这个脚本调用服务
#add by jet support APK owned root right begin
service execmd /system/bin/execmd.sh
class main
group root root #赋值为Root权限
oneshot
#add by jet support APK owned root right end
(3)在Z:\m8\trunk\system\core\include\private\修改android_filesystem_config.h,修改脚本的访问权限
// jet for test traceroute begin
{ 00777, AID_ROOT, AID_SHELL, "system/bin/execmd.sh" },
// jet for test traceroute end
(4)在APK里如下调用,比如在PhoneWindowManager.java里
if(keyCode == KeyEvent.KEYCODE_MEDIA_PREVIOUS)
{
Log.d(TAG, "traceroute 163");
SystemProperties.set("ctl.start", "execmd:/system/xbin/busybox traceroute www.163.com"); //execmd是服务,冒号后面是需要执行的命令
}
(5)执行完命令后是看不到执行的Log的,因为服务和Android不是同一个进程。所以默认生成的结果放到
echo "======start cmd====="
cmd=$@
$cmd > /sdcard/execmd //默认的路径,可修改。
echo "stopped" >> /sdcard/execmd
echo "======end $cmd====="
(6)当然可以通过创建filestream来读取结果。