当前位置: 首页 > wzjs >正文

杭州网站建设那家好优化推广网站怎么做

杭州网站建设那家好,优化推广网站怎么做,软件应用开发,广西智能网站建设找哪家关于之前说的native service 之前有写过类似的文章,今天主要介绍下如何通过binder 方式跨进程调用和回调,结合网上的各种文章,总结了3种常见的添加方式,供大家参考。 一,aidl 文件定义 先看下整体的目录结构 libserviceaidl 就是…

关于之前说的native service 之前有写过类似的文章,今天主要介绍下如何通过binder 方式跨进程调用和回调,结合网上的各种文章,总结了3种常见的添加方式,供大家参考。

一,aidl 文件定义

先看下整体的目录结构

libserviceaidl 就是我定义的aidi接口

IService.aidl

package com.test;import com.test.IServiceCallback;interface IService {void registerCallback(IServiceCallback callback);void call(int type);int  callMcu(int file,in byte[] type); 
}

IServiceCallback.aidl

package com.test;interface IServiceCallback{void onEventString(int eventCode, String eventMessage);void onEventbyte(out byte[] data);
}

通过Android.bp 编译成so

cc_library_shared {name: "libserviceaidl",aidl: {export_aidl_headers: true,local_include_dirs: ["aidl"],include_dirs: [],},srcs: [":serviceaidl",],shared_libs: ["libbase","libcutils","libutils","liblog","libbinder","libgui",],cflags: ["-Werror","-Wall","-Wextra",],
}filegroup {name: "serviceaidl",srcs: ["aidl/com/test/IService.aidl","aidl/com/test/IServiceCallback.aidl",],path: "aidl",
}

make libserviceaidl 

会生成libserviceaidl.so ,需要push 到我们系统对应目录,这样调用的binder 库就定义好了。

二,编译Native service 

Android.bp 添加aidl 库依赖

cc_binary {name: "testservverfirst",srcs: ["servertest.cpp",],shared_libs: ["liblog","libcutils","libutils","libbinder","libserviceaidl",// 上面编译生成的],init_rc: ["testservver.rc"],
}cc_binary {name: "clienttest",srcs: ["clienttest.cpp", ],shared_libs: ["liblog","libcutils","libutils","libbinder","libserviceaidl",],
}

servertest.cpp 源文件

#define LOG_TAG "testservverfirst"
#include <log/log.h>#include <unistd.h>
#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <termios.h> 
#include <utils/String16.h>
#include <com/test/BnService.h>using namespace std;
using namespace android;class TestService : public com::test::BnService
{public:TestService(){}binder::Status nativecall(int i) {ALOGI("server nativecall function is running %d",i);if(mycallback != nullptr){mycallback->onEventString(4,String16("callbacksuccuess"));}return binder::Status();}binder::Status  callMcu(int32_t file,const vector<uint8_t>& type, int32_t* _aidl_return){*_aidl_return = 1;ALOGI("server callMcu function is running _aidl_return",*_aidl_return);return binder::Status();}binder::Status  call(int32_t type){ALOGI("server call function is running call",type);return binder::Status();}// 实现回调注册binder::Status registerCallback(const sp<com::test::IServiceCallback>& callback){mycallback  = callback;return binder::Status();}private:sp<::com::test::IServiceCallback> mycallback;};int main(int argc, char const *argv[])
{ALOGD("Server is runing");//发布服务defaultServiceManager()->addService(String16("TestService"), new TestService());ProcessState::self()->startThreadPool();IPCThreadState::self()->joinThreadPool();return 0;
}

编译testservverfirst ,会生成testservverfirst 可执行文件,我们push 到系统/system/bin/ 下

手动执行:./system/bin/testservverfirst 会看到

01-18 17:59:16.174  4144  4144 D testservverfirst: Server is runing

说明我们的native service 运行成功了,也可以通过命令查看:

查看native service 是否运行成功

device:/ # ps -A| grep testservverfirst                                                                                                                                                                  
root           4144   3449   15340   4952 binder_ioctl_write_read 0 S testservverfirst

查看binder service 是否运行成功

130|monaco_go:/ # dumpsys | grep TestService,                                                                                                                                                               
--------- 0.009s was the duration of dumpsys TestService, ending at: 2025-01-18 18:18:52
 

三,编译client 

我们编译一个client 来本地验证下,binder 通信是否成功

make  clienttest

#define LOG_TAG "clienttest"
#include <log/log.h>#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <com/test/BnService.h>using namespace android;int main(int argc, char const *argv[])
{sp<IServiceManager> sm = defaultServiceManager();sp<IBinder> binder = sm->getService(String16("TestService"));sp<com::test::IService> simservice = interface_cast<com::test::IService>(binder);simservice->call(3);return 0;
}


生成的clienttest ,push 到系统system/bin/,手动执行 ./system/bin/clienttest,查看service 的log 

01-18 18:00:25.336  4144  4145 I testservverfirst: server call function is running call

说明调用成功了。

四,添加开机启动

这里我们加了一个rc 文件,开机会解析。

testservver.rc

on post-fschown system system /system/bin/testservverfirstchmod 0777 /system/bin/testservverfirstrestorecon_recursive /system/bin/testservverfirstservice testservverfirst /system/bin/testservverfirstclass coreconsoledisabledseclabel u:r:su:s0on property:sys.xss=2setprop xss.home 2start testservverfirst  on property:sys.boot_completed=1start testservverfirst  

这里我们监听sys.boot_completed 开机完成的属性,来启动服务,还有其他的方式,参考之前写的文章

Android 新增可执行脚本方式总结_android脚本-CSDN博客

五,增加selinux 权限

上面四个步骤,我们走完过后,发现开机服务是启动不了的,因为缺少selinux 权限,这个之前文章也介绍过,这个需要耐心,慢慢添加,大概步骤如下。

主要修改

file_contexts  添加native service 标签

testservverfirst.te 添加native 权限

service.te 添加binder service 类型

service_contexts 添加binder service 标签

调试的时候记得先关闭selinux 这样方便一次性把权限都加完。

diff --git a/prebuilts/api/30.0/private/testservverfirst.te b/prebuilts/api/30.0/private/testservverfirst.te
new file mode 100644
index 0000000..5a72981
--- /dev/null
+++ b/prebuilts/api/30.0/private/testservverfirst.te
@@ -0,0 +1,15 @@
+type testservverfirst, domain,mlstrustedsubject;
+typeattribute testservverfirst coredomain;
+type testservverfirst_exec, system_file_type, exec_type, file_type;
+binder_use(testservverfirst)
+init_daemon_domain(testservverfirst)
+
+allow testservverfirst servicemanager:binder { call transfer };
+allow testservverfirst testservverfirst_service:service_manager { add find };
+allow testservverfirst tty_device:chr_file { read write };
+allow testservverfirst untrusted_app:binder call;
+
+binder_use(testservverfirst)
+add_service(testservverfirst,testservverfirst_service)
+
+
diff --git a/prebuilts/api/30.0/private/file_contexts b/prebuilts/api/30.0/private/file_contexts
index b774920..37049f6 100644
--- a/prebuilts/api/30.0/private/file_contexts
+++ b/prebuilts/api/30.0/private/file_contexts
@@ -738,3 +738,4 @@############################## mount point for read-write product partitions/mnt/product(/.*)?          u:object_r:mnt_product_file:s0
+/system/bin/testservverfirst u:object_r:testservverfirst_exec:s0
diff --git a/prebuilts/api/30.0/private/service.te b/prebuilts/api/30.0/private/service.te
index 6c17521..77d585a 100644
--- a/prebuilts/api/30.0/private/service.te
+++ b/prebuilts/api/30.0/private/service.te
@@ -6,3 +6,4 @@type statscompanion_service,        system_server_service, service_manager_type;type statsmanager_service,          system_api_service, system_server_service, service_manager_type;type uce_service,                   service_manager_type;
+type testservverfirst_service,        service_manager_type;
diff --git a/prebuilts/api/30.0/private/service_contexts b/prebuilts/api/30.0/private/service_contexts
index 7c9f396..c1687f7 100644
--- a/prebuilts/api/30.0/private/service_contexts
+++ b/prebuilts/api/30.0/private/service_contexts
@@ -249,3 +249,4 @@window                                    u:object_r:window_service:s0glasses                                   u:object_r:sim_glasses_service:s0*                                         u:object_r:default_android_service:s0
+TestService                            u:object_r:testservverfirst_service:s0
diff --git a/prebuilts/api/30.0/private/untrusted_app.te b/prebuilts/api/30.0/private/untrusted_app.te
index 6e7a99c..97a84e3 100644
--- a/prebuilts/api/30.0/private/untrusted_app.te
+++ b/prebuilts/api/30.0/private/untrusted_app.te
@@ -14,3 +14,5 @@untrusted_app_domain(untrusted_app)net_domain(untrusted_app)bluetooth_domain(untrusted_app)
+allow untrusted_app testservverfirst:binder { transfer call };
+allow untrusted_app testservverfirst_service:service_manager find;
diff --git a/private/testservverfirst.te b/private/testservverfirst.te
new file mode 100644
index 0000000..5a72981
--- /dev/null
+++ b/private/testservverfirst.te
@@ -0,0 +1,15 @@
+type testservverfirst, domain,mlstrustedsubject;
+typeattribute testservverfirst coredomain;
+type testservverfirst_exec, system_file_type, exec_type, file_type;
+binder_use(testservverfirst)
+init_daemon_domain(testservverfirst)
+
+allow testservverfirst servicemanager:binder { call transfer };
+allow testservverfirst testservverfirst_service:service_manager { add find };
+allow testservverfirst tty_device:chr_file { read write };
+allow testservverfirst untrusted_app:binder call;
+
+binder_use(testservverfirst)
+add_service(testservverfirst,testservverfirst_service)
+
+
diff --git a/private/file_contexts b/private/file_contexts
index b774920..37049f6 100644
--- a/private/file_contexts
+++ b/private/file_contexts
@@ -738,3 +738,4 @@############################## mount point for read-write product partitions/mnt/product(/.*)?          u:object_r:mnt_product_file:s0
+/system/bin/testservverfirst u:object_r:testservverfirst_exec:s0
diff --git a/private/service.te b/private/service.te
index 6c17521..77d585a 100644
--- a/private/service.te
+++ b/private/service.te
@@ -6,3 +6,4 @@type statscompanion_service,        system_server_service, service_manager_type;type statsmanager_service,          system_api_service, system_server_service, service_manager_type;type uce_service,                   service_manager_type;
+type testservverfirst_service,        service_manager_type;
diff --git a/private/service_contexts b/private/service_contexts
index 7c9f396..c1687f7 100755
--- a/private/service_contexts
+++ b/private/service_contexts
@@ -249,3 +249,4 @@window                                    u:object_r:window_service:s0glasses                                   u:object_r:sim_glasses_service:s0*                                         u:object_r:default_android_service:s0
+TestService                            u:object_r:testservverfirst_service:s0
diff --git a/private/untrusted_app.te b/private/untrusted_app.te
index 6e7a99c..97a84e3 100644
--- a/private/untrusted_app.te
+++ b/private/untrusted_app.te
@@ -14,3 +14,5 @@untrusted_app_domain(untrusted_app)net_domain(untrusted_app)bluetooth_domain(untrusted_app)
+allow untrusted_app testservverfirst:binder { transfer call };
+allow untrusted_app testservverfirst_service:service_manager find;

http://www.dtcms.com/wzjs/411205.html

相关文章:

  • html网站后台管理系统外包seo服务口碑好
  • 电商建设网站客户引流的最快方法是什么
  • 新建的网站怎么做seo优化什么平台引流最快
  • 建行国际互联网网站优化搜索引擎
  • 政府网站栏目建设反馈意见搜索热门关键词
  • linux下可以用wordpress武汉外包seo公司
  • 香港空间做的网站接受大陆监管么seo外包优化
  • 黔南住房和城乡建设委员会网站济南专业做网站
  • 网站建设服务怎么样seo项目是什么
  • 学做网站要多久年度关键词
  • 网站建设 app开发网站邯郸网站建设优化
  • 网站开发纠纷案外贸建站服务推广公司
  • 水利部建设与管理司网站长沙seo关键词排名
  • 小地方网站建设公司网站优化排名首页
  • 中小型企业查询河北百度seo
  • 网站建设费发票名称百度app客服人工电话
  • 手机上免费自己做网站湖南企业竞价优化首选
  • 如何自己网站接装修生意做企业官方网站有哪些
  • 晋中公司做网站制作网页的教程
  • 做影视网站需要多大硬盘免费个人网站平台
  • 湖南省网站建设项目长沙正规seo优化公司
  • 青岛做网站的公司哪个比较好营销策略主要包括哪些
  • 企业网站建设方法福州seo网站推广优化
  • 单页移动网页建站教程舆情分析网站免费
  • 网站没域名谷歌seo优化排名
  • 广州小程序开发的公司合肥seo推广公司哪家好
  • 浅谈政府网站集约化建设百度注册入口
  • 网站设计建设企业制作网页app
  • 网络舆情监测与研判考试重点国外网站谷歌seo推广
  • 大埔做网站腾讯广告投放平台官网