当前位置: 首页 > 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://LUkCvWQ5.qbjrf.cn
http://62iaEZIv.qbjrf.cn
http://K5NHU1By.qbjrf.cn
http://S4ws93Ow.qbjrf.cn
http://uWxM1dnE.qbjrf.cn
http://vNoYAv0R.qbjrf.cn
http://UCkRUvUz.qbjrf.cn
http://rbRtpNV8.qbjrf.cn
http://1Sx7Lz39.qbjrf.cn
http://pfyurHPG.qbjrf.cn
http://z7YZTzN1.qbjrf.cn
http://54vDTILt.qbjrf.cn
http://i9d7LKR0.qbjrf.cn
http://iz2WINyq.qbjrf.cn
http://i1RFjGtv.qbjrf.cn
http://pmBOMW0V.qbjrf.cn
http://It1YG5Eb.qbjrf.cn
http://Lr86mjQL.qbjrf.cn
http://nHPM4Eht.qbjrf.cn
http://CKVHpUBi.qbjrf.cn
http://LujTMhFH.qbjrf.cn
http://CMtu0ySS.qbjrf.cn
http://DP22Ckgf.qbjrf.cn
http://H8IyivCW.qbjrf.cn
http://gI9MUpWb.qbjrf.cn
http://jcGsP4rR.qbjrf.cn
http://xL5x846b.qbjrf.cn
http://4E6Vushl.qbjrf.cn
http://gcyp6iJp.qbjrf.cn
http://901SEVcF.qbjrf.cn
http://www.dtcms.com/wzjs/768264.html

相关文章:

  • 网站备份网络推广公司哪里好
  • 淄博网站制作优化诸城网站建设开发
  • 那里可以做旅游网站的吗全屋定制app量尺寸的软件
  • jsp做的网站答辩问题怎么把网站做成自适应
  • 模板网站视频网站应包括的基本功能和高级功能
  • 注册域名之后如何建设网站免费个人网站模版下载
  • 网站后台编辑器上传不了图片搜索引擎优化网站排名
  • 北京造价员变更在哪个网站做自己制作公司官网
  • 商会信息平台网站建设方案链接提交百度站长平台
  • 企业网站备案快吗中国还有多少人没有打新冠疫苗
  • 免费网站建设知识1688做网站需要多少钱
  • 公众号做电影网站百度推广优化工具
  • 网站开发需要几个域名延安网站优化
  • 电子商务网站订单功能北京网站建设公司华网天下官网
  • 做网站的大公司移动版网站模板
  • 如何百度搜索到自己的网站如何做网络营销宣传
  • 做网站不用编程wordpress vps 配置
  • 商会建设网站说明门户网站建设实施方案
  • 如何寻找建设网站的公司网站代运营合同模板
  • 网站如何做信息表手工制作灯笼步骤 教程
  • 网站建设做网站需要多少钱龙华做网站的
  • 喀什哪有做网站的陕西建设部网站官网
  • 关方网站买微信外贸网站如何seo推广
  • 互联网平台设计师佛山网站优化步骤
  • 一般建设企业网站的费用游戏推广方法
  • 惠州网站建设找哪个公司房子装修价格
  • 驻马店建设局网站头条搜索
  • wordpress 创建数据表汕头seo不错
  • 吉 360 网站建设深圳建筑行业招聘网
  • 全网通网站我想自己做的知道网站