我的Android应用程序如何与本机守护程序通信? [英] How can my Android app communicate with a native daemon?

查看:69
本文介绍了我的Android应用程序如何与本机守护程序通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个作为系统运行的自定义Android应用程序(在ROM中作为系统应用程序构建).我已经编写了一个本机守护程序,该守护程序也可以正常运行,并以root身份运行.但是,我想知道两者之间如何沟通?我知道我可以让本机守护程序创建一个网络套接字并连接到应用程序,但是将本地套接字用于IPC似乎更好,但是我正在努力寻找示例代码.

So, I have a custom Android application running as System (built as a system app in my ROM). And Ive written a native daemon that works fine too and runs as root. However, I was wondering how the two can communicate? I know I can have the native daemon create a network socket and have the App connect to it, but using a local socket for IPC seems even better, but I am struggling to find example code for it.

我认为我可以按照这里,但我不知道如何在Java中与之通信.

I figured I could create the socket like as described here but I have no idea how to communicate with it in Java.

任何帮助将不胜感激.

Any help would be much appreciated.

此外,起初我编写了一个本机库,并使用JNI从Java与之通信,但是很快意识到它显然是在应用程序的上下文中运行的,因此它不能以root身份运行,这是我的要求,并且本机守护程序可以做得很好.在这里使用插座是最好的选择,还是Binder也可以工作?

Also, at first I wrote a native library and used JNI to communicate with it from Java, but quickly realized that it obviously runs in the context of the app, so it cant be run as root, which is a requirement I have, and something the native daemon can do fine. Is using sockets the best alternative here or would Binder work too?

一吨!

推荐答案

此处的解决方案是查看AOSP并查看installd如何与Installer java应用程序通信.在init.rc中,您将看到如何创建命名的本地套接字;在installd.c中,您将看到本机代码如何与套接字通信.在Installer.java中,您可以看到它是如何从Java平台完成的.

The solution here was to look at AOSP and see how installd communicates with the Installer java app. In init.rc you see how the named local socket is created and in installd.c you see how native code communicates with the socket. In Installer.java you see how its done from Java land.

不要认为Binder将是适合我特定用例的正确方法.

Dont think Binder would be the right way to go for my particular use case.

这篇关于我的Android应用程序如何与本机守护程序通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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