Android APK 中启动特定的 bin 服务
来源:互联网 发布:淘宝衣服评价语 编辑:程序博客网 时间:2024/04/30 13:14
日前工作中遇到一个需求:要求在启动App时,启动对应的bin 服务,从而使得实现APK作为Client端,而bin 服务作为 Server端 实现两者socket通信。
首先bin 服务文件是由同事A君负责用C语言编写实现,完成后编译到Android手机的system/bin 目录下,名称为:cloudtestsuited ;
至于APK则是由本人负责编写实现,利用socket实现两者的通信。
为了实现打开apk即打开了对应的bin服务,故需要实现在apk中启动bin服务需求。
在本案中是利用通过init.rc启动系统服务来运行对应的bin服务文件;
1.Android启动文件系统后调用的会调用第一个应用程序是/init,此文件一个很重要的内容就是解析了init.rc和init.xxx.rc,然后执行解析出来的任务。而init.rc,可以在系统的初始化过程中进行一些简单的初始化操作。利用这一点,可以编写简单的关机或重启的sh脚本文件,通过系统init解析,执行相应的操作。
以mtk 6795平台工程为例(后文都是此平台),在device/amt/amt6795_evb_m/init.project.rc 文件中添加如下代码:
on post-fs-data //在此节点下添加如下代码 chmod 0777 /system/bin/cloudtestsuitedon init // 在此节点下添加如下代码service cloudtestsuited /system/bin/cloudtestsuited class main oneshot disabled
oneshot选项表示该服务只启动一次,而如果没有oneshot选项,这个可执行程序会一直存在--如果可执行程序被杀死,则会重新启动。
disabled 表示禁用服务,此服务开机时不会自动启动,但是可以在应用程序中手动启动它。
参考代码文件:
2.添加对应的selinux权限
在device\mediatek\common\sepolicy\file_contexts 文件中添加bin服务对应的权限:
# Add by silead begin/system/bin/cloudtestsuited u:object_r:fpsvcd_exec:s0 //此行# Add by silead end在device\mediatek\common\sepolicy\system_app.te 文件中添加bin服务对应的权限:
# Add by silead 2016/03/18 begin# add for fp.apk create file under '/data/silead/' file pathallow system_app fpsvcd_data_file:dir { create write add_name remove_name read open search};allow system_app fpsvcd_data_file:file { unlink getattr create write open read };# add for starting cloudtestsuited in apkallow system_app fpsvcd_tmpfs:file { read write open getattr };allow system_app fpsvcd_exec:file { getattr read execute open execute_no_trans };allow system_app fpsvcd:dir { read open };allow system_app tmpfs:dir { read write getattr };# Add by silead 2016/03/18 end在device\mediatek\common\sepolicy\ 目录下新增一个权限文件fpsvcd.te:
type fpsvcd_exec , exec_type, file_type;type fpsvcd ,domain;init_daemon_domain(fpsvcd)allow fpsvcd fpsvcd:capability { sys_nice dac_override net_admin};allow fpsvcd fpsvcd:capability2 { block_suspend };allow fpsvcd init_tmpfs:file {getattr read open};allow fpsvcd tmpfs:dir { write add_name };allow fpsvcd fpsvcd_tmpfs:file { write create read open};allow fpsvcd silead_fp_dev_device:chr_file { read write ioctl open };allow fpsvcd uhid_device:chr_file { write open ioctl};allow fpsvcd fpsvcd:netlink_kobject_uevent_socket {create read write setopt bind };allow fpsvcd sysfs_wake_lock:file {write read open};allow fpsvcd system_app:dir { read open };#allow fpsvcd system_data_file:dir { create write add_name read open };#allow fpsvcd system_data_file:file { create write open read };allow fpsvcd fpsvcd_data_file:dir { create write add_name remove_name read open search};allow fpsvcd fpsvcd_data_file:file {unlink getattr create write open read };#allow fpsvcd silead_data_file:dir { search create write add_name read open };#allow fpsvcd silead_data_file:file { create write open read getattr };allow fpsvcd platform_app_tmpfs:file { open read write };allow fpsvcd fingerprintd:dir { open read };allow fpsvcd fuse:dir { search open add_name remove_name read write create};allow fpsvcd fuse:file { unlink open read write create getattr setattr rename};allow fpsvcd property_socket:sock_file { write };allow fpsvcd init:unix_stream_socket { connectto };allow fpsvcd system_prop:property_service { set };具体参考代码:
一,可以在init.rc中在声明service 前 添加一行代码,
on property:sys.service.silead=enabled
start cloudtestsuited
然后再apk中就可以执行如下代码,前提是apk具有platform签名,system权限:
//启动bin服务
case
R.id.shutdown_btn2:
Log.v(TAG,
"system service->shutdown"
);
SystemProperties.set(
"sys.service.silead"
,
"enabled"
);
break
;
参考代码:
以上即可实现在apk中调用启动bin服务文件。
具体apk实现代码可以参考 FpCloudTest.zip 附件: http://download.csdn.net/detail/jiuxiaoyunwu/9499487
相关selinux权限 即te权限的添加请参考:http://download.csdn.net/detail/jiuxiaoyunwu/9499570
- Android APK 中启动特定的 bin 服务
- Android APK 中启动特定的 bin 服务
- Android在init.rc中加入脚本开机安装APK并启动APK的服务
- Android项目中把bin文件夹里面的.apk文件删除,怎么让它再生成
- SystemServer中启动的android服务
- Android中常用的Intent启动服务
- 启动apk或者服务的常用方法
- android 启动另一个apk中acitity的方法
- Android中怎么计算apk的启动时间
- Android apk的启动过程
- apk中启动apk
- apk中启动apk
- Android系统预制APK、so和bin的方法
- 一个apk中启动另个apk的启动代码
- Android项目在eclipse中无法打包apk文件[bin目录下没生成apk文件]
- android中根据包名启动apk
- android中启动第三方apk
- android中实现暗码启动apk【一】
- 凭证处理被批次处理冻结
- iOS 自定义常见第三方分享
- 关于百度地图申请key和混淆说明
- vs2013 创建调用链接库 没有生成 “.lib” 问题
- 函数类型的变量
- Android APK 中启动特定的 bin 服务
- 利用spring的profile切换不同的环境
- cocos2dx——scheduleOnce中回调delay函数添加参数
- Android中优秀控件学习
- ubuntu开启SSH服务
- smtp协议的基本命令
- Shell编程中Shift的用法
- OpenGL的版本历史和发展
- 如何在windows系统的cmd命令行下调用VS编译器